【问题标题】:GS1-128 and RegExGS1-128 和正则表达式
【发布时间】:2013-06-28 10:09:04
【问题描述】:

我正忙于 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,将其分为以下几组:

  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 组设为可选时,对于所有确实包含 AI 30 的条形码,这些组的内容都会被放入第 7 组。

如何将 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 从何处开始,条形码本身会省略这些。

【问题讨论】:

  • 关于第 8 节和第 9 节的检测:如果您必须使用字符串操作(没有 REGEX)以编程方式执行此操作,您会怎么做?这对我来说似乎很模棱两可。
  • 尝试让第 7 组不贪婪,例如 \d{1,20}?。这优先于第 8 组。
  • 想一想:第 7 组是否有可能在其 1 到 20 个字符中包含 30
  • @Quatroking 我不是从 REGEX 匹配的角度问的。我的意思是:如果第 7 组真的包含30,然后我们可以有另一个30(可选)怎么办?这可能会使规范模棱两可。
  • @Quatroking,如果你能提供一些测试用例和每个测试用例的预期结果,那就太好了。

标签: c# regex barcode code128


【解决方案1】:

试试这个:

^(?<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) «$»
-->

编辑

省略转义括号:

^(?<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})?)$

【讨论】:

  • 我感觉测试用例中包含的括号只是为了强调组之间的区别。从 OP 的 REGEX 来看,我认为它们不包含在真实数据中。
  • w0lf 是正确的,括号仅代表 AI。但是,我从您的正则表达式中删除了括号并添加了我自己的公司代码,它运行良好!谢谢!编辑:实际上,在审查之后,删除括号会导致 AI 30 再次退回到 AI 21/10:/
【解决方案2】:

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

如果它不存在,我会找出它被剥离的位置并防止它被剥离。

【讨论】:

  • 同意。 FNC1 字符为 ASCII 29 或正则表达式 \x1D
【解决方案3】:

试试这个,它可以为您提供所有细分的匹配组 前任。 (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条件。

【讨论】:

    猜你喜欢
    • 2019-01-29
    • 2016-09-13
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多