【问题标题】:How to get specific user code from address label如何从地址标签中获取特定的用户代码
【发布时间】:2021-11-14 22:39:15
【问题描述】:

我正在尝试从包含地址和 6 位数字的包裹标签中获取用户代码,有时还包含 2 个带有 6 位数字的前缀。首先,我得到标签图像,然后在 aws textract 的帮助下,我获取文本。但在文本内部有时可能有 6 位数字的另一个代码。

我用preg_match_all 尝试了(\s\d{6}\s)|((\.)\d{6}\s)|(\s[a-zA-Z]{2}\d{6}\s) 正则表达式。

是否有任何解决方案可以帮助我找到该代码?

请注意,地址始终是静态的,可能有任何函数可以在该地址附近进行搜索?

标签示例。搜索到-->

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt --> 913847 <-- ut labore et dolore magna aliqua.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua --> 913847 <--.
--> TK913847 <-- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

【问题讨论】:

  • 喜欢preg_match_all('~\b(?:[a-zA-Z]{2})?\d{6}(?!\S)~', $text, $matches)?
  • 您可以在 --> 处拆分字符串,然后在
  • @prasadK 我已添加 -->

标签: php regex laravel amazon-web-services


【解决方案1】:

使用

preg_match_all('/address\s+\K(?:[A-Z]{2})?\d{6}\b/i', $string, $matches)

注意:不是preg_match。使用 preg_match_all 从您的文本中获取所有匹配项。

regex proof

解释

--------------------------------------------------------------------------------
  address                  'address'
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \K                       match reset operator
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    [A-Z]{2}                 any character of: 'A' to 'Z', 'a' to 'z' (2 times)
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  \d{6}                    digits (0-9) (6 times)
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char

【讨论】:

    【解决方案2】:

    看起来这可以满足您的所有需求:

    preg_match_all('#\b(([\w]{2})?[\d]{6})\b#ms', $input, $matches);
    

    匹配以下代码示例:

    • AA123456
    • bb123456
    • 123456

    但如果由于以下边界而成为术语的一部分,则不会匹配:

    • lorem123456
    • code123456aa

    【讨论】:

    • 如我所说,我已经使用了 preg match。但问题是可能还有另一个不是用户代码的 6 位数字。另一件事是我正在使用该代码获取用户数据,并且我应该检查该代码的公司可能超过 5-6 家。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2021-12-14
    • 2018-08-05
    • 2011-08-29
    相关资源
    最近更新 更多