首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GS1-128和RegEx

GS1-128和RegEx
EN

Stack Overflow用户
提问于 2013-07-01 10:10:10
回答 3查看 5.4K关注 0票数 4

我正在忙于GS1-128,并希望匹配扫描条形码使用RegEx。我目前有以下表达式:

代码语言:javascript
复制
^(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,将其分成以下组:

  1. 01 - GTIN
  2. 12345678 -公司代码(虚拟)-8位数字
  3. 12345 -部分代码(虚拟)-5位数字
  4. 11或17 -生产日期/到期日
  5. 130500 -日期-6位数字
  6. 10或21 -批次/序列号
  7. 1234567890 -1至20个字符
  8. 30 -项目计数(可选)
  9. 42-1至8个字符(可选)

现在,我有时有一个条形码,它没有AI;30项的计数。我似乎根本想不出怎么把这件事变成我的准则。每当我使第8组和第9组可选时,这些组的内容就会被抛到第7组中,因为所有包含AI 30的条形码。

如何使AI 30可选,同时防止它与AI 21/10一起分组?

测试用例:

(01)12345678654320(11)120500(21)1234567890应该提供以下匹配:

  1. 01
  2. 12345678
  3. 65432
  4. 11
  5. 120500
  6. 21
  7. 1234567890
  8. 不匹配
  9. 不匹配

(01)12345678124570(17)130700(10)30567(30)50应该提供以下匹配:

  1. 01
  2. 12345678
  3. 12457
  4. 17
  5. 130700
  6. 10
  7. 30567
  8. 30
  9. 50

(01)12345678888880(11)140200(21)66503042(30)100应该提供以下匹配:

  1. 01
  2. 12345678
  3. 88888
  4. 11
  5. 140200
  6. 21
  7. 66503042
  8. 30
  9. 100

注意,括号只显示AI的起始位置,条形码本身省略了这些.

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-07-01 11:14:40

尝试如下:

代码语言:javascript
复制
^(?<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})?)$

上述表达式应匹配以下所有项:

代码语言:javascript
复制
(01)12345678654320(11)120500(21)1234567890
(01)12345678124570(17)130700(10)30567(30)50
(01)12345678888880(11)140200(21)66503042(30)100

解释:

代码语言:javascript
复制
<!--
^(?<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) «$»
-->

编辑

省略的逃出的父母:

代码语言:javascript
复制
^(?<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})?)$
票数 2
EN

Stack Overflow用户

发布于 2013-08-07 14:48:37

可变长度的AIs应以FNC1字符结束。如果这是存在的,您应该使用它来查找\d{1,20}的结尾。

如果它不存在,我会找出它被剥去的地方,并防止它被剥去。

票数 1
EN

Stack Overflow用户

发布于 2020-02-25 08:51:34

试一试,这给你匹配组的所有段前。(3302)103300(10)L20060831A117,(02)04008577004106(15)081231(37)000025

代码语言:javascript
复制
\(\d{1,3}[0-9,a-z,A-Z]{1}?\)[\d,a-z,A-Z]{1,70}

之后,你能应用这个正则表达式吗?

代码语言:javascript
复制
\((.*?)\)

在每个片段上找到哪个AI有代码部分,然后您可以验证,代码部分是否符合他的AI的条件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17401909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档