我正忙着使用GS1-128并希望使用RegEx匹配扫描的条形码。我目前有以下表达式:
^(01)(12345678)(\d{5})\d(11|17)(\d{2}[0-1]\d[0-3]\d)(10|21)(\d{1,20})(30)(\d{1,20})
这成功匹配条形码(01)12345678123450(11)130500(21)1234567890(30)42
,将其分为以下几组:
现在,我有时会有条形码,没有AI项目的数量; 30.我似乎无法弄清楚如何将这一点用于我的正则表达式。每当我制作第8组& 9可选,对于做包含AI 30的所有条形码,这些组的内容将被抛入组7。
如何防止AI 30与AI 21/10分组?
测试用例:
(01)12345678654320(11)120500(21)1234567890
应该提供以下匹配:
(01)12345678124570(17)130700(10)30567(30)50
应该提供以下匹配:
(01)12345678888880(11)140200(21)66503042(30)100
应该提供以下匹配:
请注意,括号仅用于显示AI开始的位置,条形码本身会忽略这些。
答案 0 :(得分:2)
试试这个:
^(?<gtin>\(01\))(?<comp_code>12345678)(?<part_code>\d{5})0?(?<pd_ed>\((?:11|17)\))(?<date>\d{6})(?<bat_no>\((?:21|10)\))(?<data_req>\d{1,20}?)\b(?<count>(?:\(30\))?)(?<data_opt>(?:\d{1,8})?)$
上述表达式应与以下所有项目匹配:
(01)12345678654320(11)120500(21)1234567890
(01)12345678124570(17)130700(10)30567(30)50
(01)12345678888880(11)140200(21)66503042(30)100
说明:
<!--
^(?<gtin>\(01\))(?<comp_code>12345678)(?<part_code>\d{5})0?(?<pd_ed>\((?:11|17)\))(?<date>\d{6})(?<bat_no>\((?:21|10)\))(?<data_req>\d{1,20}?)\b(?<count>(?:\(30\))?)(?<data_opt>(?:\d{1,8})?)$
Assert position at the beginning of the string «^»
Match the regular expression below and capture its match into backreference with name “gtin” «(?<gtin>\(01\))»
Match the character “(” literally «\(»
Match the characters “01” literally «01»
Match the character “)” literally «\)»
Match the regular expression below and capture its match into backreference with name “comp_code” «(?<comp_code>12345678)»
Match the characters “12345678” literally «12345678»
Match the regular expression below and capture its match into backreference with name “part_code” «(?<part_code>\d{5})»
Match a single digit 0..9 «\d{5}»
Exactly 5 times «{5}»
Match the character “0” literally «0?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the regular expression below and capture its match into backreference with name “pd_ed” «(?<pd_ed>\((?:11|17)\))»
Match the character “(” literally «\(»
Match the regular expression below «(?:11|17)»
Match either the regular expression below (attempting the next alternative only if this one fails) «11»
Match the characters “11” literally «11»
Or match regular expression number 2 below (the entire group fails if this one fails to match) «17»
Match the characters “17” literally «17»
Match the character “)” literally «\)»
Match the regular expression below and capture its match into backreference with name “date” «(?<date>\d{6})»
Match a single digit 0..9 «\d{6}»
Exactly 6 times «{6}»
Match the regular expression below and capture its match into backreference with name “bat_no” «(?<bat_no>\((?:21|10)\))»
Match the character “(” literally «\(»
Match the regular expression below «(?:21|10)»
Match either the regular expression below (attempting the next alternative only if this one fails) «21»
Match the characters “21” literally «21»
Or match regular expression number 2 below (the entire group fails if this one fails to match) «10»
Match the characters “10” literally «10»
Match the character “)” literally «\)»
Match the regular expression below and capture its match into backreference with name “data_req” «(?<data_req>\d{1,20}?)»
Match a single digit 0..9 «\d{1,20}?»
Between one and 20 times, as few times as possible, expanding as needed (lazy) «{1,20}?»
Assert position at a word boundary «\b»
Match the regular expression below and capture its match into backreference with name “count” «(?<count>(?:\(30\))?)»
Match the regular expression below «(?:\(30\))?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the character “(” literally «\(»
Match the characters “30” literally «30»
Match the character “)” literally «\)»
Match the regular expression below and capture its match into backreference with name “data_opt” «(?<data_opt>(?:\d{1,8})?)»
Match the regular expression below «(?:\d{1,8})?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single digit 0..9 «\d{1,8}»
Between one and 8 times, as many times as possible, giving back as needed (greedy) «{1,8}»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
-->
省略逃脱的parens:
^(?<gtin>01)(?<comp_code>12345678)(?<part_code>\d{5})0?(?<pd_ed>(?:11|17))(?<date>\d{6})(?<bat_no>(?:21|10))(?<data_req>\d{1,20}?)\b(?<count>(?:30)?)(?<data_opt>(?:\d{1,8})?)$
答案 1 :(得分:0)
可变长度AI应以FNC1字符终止。如果存在,您应该使用它来查找\d{1,20}
的结尾。
如果它不存在,我会发现它被剥离的位置并防止它被剥离。
答案 2 :(得分:0)
尝试这一步,这将为您匹配所有细分受众群 例如(3302)103300(10)L20060831A117,(02)04008577004106(15)081231(37)000025
\(\d{1,3}[0-9,a-z,A-Z]{1}?\)[\d,a-z,A-Z]{1,70}
之后,您可以应用此正则表达式吗?
\((.*?)\)
在每个段上查找哪个AI具有代码部分 然后您可以验证代码部分是否满足其AI条件。