在使用反向引用的正则表达式表达方面需要帮助 [英] Need help with regex expression using backreferences

查看:84
本文介绍了在使用反向引用的正则表达式表达方面需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是使用regex表达式的新手.我正在尝试解析一些EDI数据.我的匹配模式如下所示:
trn(?ed).){1,2} \ k(ed)(?ref .. {1,30})(\ k(ed).{10,10} | \ k(ed) ;.{10,10} \ k<.{1,30})?(?< sd>.{0,1})

对于此特定的数据字符串,我最多有4个元素字段,这些元素字段由元素定​​界符分隔.该字符串将始终包含"TRN",后跟元素定界符,最多4个元素字段和一个段定界符.我为元素定界符添加了ed反向引用,为第二个元素字段添加了ref反向引用,为段分隔符添加了sd反向引用.第一和第二元素字段是必需的,而第三和第四元素字段是可选的.元素和段定界符永远不能相同.

样本字符串值:705TRN * 1 * 811269480000000 * 1000033492 \ 066636640 00018932955

1:第一个元素字段.长度可以是1到2个字符,可以是数字或字母的混合.

*:元素字段定界符.

811269480000000:第二个元素字段.长度可以是1到30个字符.

1000033492:第三个元素字段.长度为10.

\:应该是分段定界符,但由于某种原因它正在拉6.

066636640 00018932955:我不在乎的正在传递的其他信息.

这是我得到的比赛信息.我期望ref backreference仅引用811269480000000,而不是元素定界符和3rd元素字段的整个值.有人可以帮我弄清楚我在做什么错吗?在整个字符串中,元素定界符应始终为相同的值.如果您需要我为您提供任何其他信息,请告诉我.预先感谢您的帮助!
匹配:TRN * 1 * 811269480000000 * 1000033492 \ 06
元素定界符:*
分段厚度:6
ref:811269480000000 * 1000033492 \ 0

Hello, I am fairly new to using regex expressions. I am trying to parse some EDI data. My match pattern looks like the following:
trn(?<ed>.).{1,2}\k<ed>(?<ref>.{1,30})(\k<ed>.{10,10}|\k<ed>.{10,10}\k<ed>.{1,30})?(?<sd>.{0,1})

For this specific string of data, I have up to 4 element fields that are separated by an element delimiter. The string will always contain "TRN" followed by the element delimiter, up to 4 element fields, and a segment delimiter. I added an ed backreference for the element delimiter, an ref backreference for the 2nd element field, and an sd backreference for the segment delimeter. The 1st and 2nd element fields are required whereas the 3rd and 4th element fields are optional. The element and segment delimiters should never be the same.

Sample String Value:705TRN*1*811269480000000*1000033492\ 066636640 00018932955

1:First element field. Length can be 1 to 2 chars with a mixture of numbers or letters.

*: element field delimiter.

811269480000000: 2nd element field. Length can be 1 to 30 chars.

1000033492: 3rd element field. Length is 10.

\: Should be the segment delimiter but for some reason it is pulling the 6.

066636640 00018932955: Extra information that is being passed that I do not care about.

Here is the match information that I am getting. I am expecting the ref backreference to just be referencing 811269480000000 and not the whole value of the element delimiter and the 3rd element field. Can someone please help me figure out what I am doing wrong? The element delimiter should always be the same value throughout the whole string. Please let me know if you need for me to provide you with any other information. Thanks in advance for your help!
Match:TRN*1*811269480000000*1000033492\ 06
element delimiter:*
segment delimeter:6
ref:811269480000000*1000033492\ 0

推荐答案

该网站可能会帮助您...
http://regexlib.com/?AspxAutoDetectCookieSupport=1 [
This site may help you...
http://regexlib.com/?AspxAutoDetectCookieSupport=1[^]


这篇关于在使用反向引用的正则表达式表达方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆