PHP正则表达式匹配字符串中的SKU(几种模式)

0yycz8jy  于 2023-01-24  发布在  PHP
关注(0)|答案(2)|浏览(113)

有些记录名称中混合了几种类型的SKU,可能包含符号、数字等。
示例:

Name of product 67304-4200-52-21
67304-4200-52 Name of product
67304-4200 Name of product
38927/6437 Name of product
BKK1MBM06-02 Name of product
BKK1MBM06 Name of product

我需要preg_match(PHP)只SKU部分与任何符号在任何组合。
所以我写了模式:

/\d+\/\d+|\d+-?\d+-?\d+-?\d+|\bbkk.*\b/i

它可以工作,但不适用于[BKK *] SKU。
是否可以将所有这些类型的SKU组合在一个模式中?
谢谢。

3hvapo4f

3hvapo4f1#

模式\d+-?\d+-?\d+-?\d+意味着应该至少有4个数字,因为所有连字符都是可选的,但是在示例数据中,带有数字的部分至少有一个连字符,并且由2、3或4个部分组成。
您可以将数字和连字符部分重复1次或更多次,并且不使用.*\b,而是使用\S*\b来匹配可选的非空格字符,这些字符将回溯到最后一个单词边界。
请注意,如果您在php中使用/以外的分隔符,则不必转义\/
使用不区分大小写的匹配:

\b(?:\d+(?:-\d+)+|bkk\S*|\d+\/\d+)\b
    • 说明**
  • \b防止部分字匹配的字边界
  • (?:备选项的非捕获组
  • \d+(?:-\d+)+匹配1+位数,重复匹配- 1次或多次,然后再次匹配1+位数(或使用{1,3}代替+
  • |
  • bkk\S*匹配bkk和可选的非空白字符
  • |
  • \d+\/\d+匹配1+位数/和1+位数
  • )关闭非捕获组
  • \b字边界

参见regex101 demo

rryofs0p

rryofs0p2#

用途

\d+(?:\d+(?:-?\d+){3}|\/\d+)|\b[bB][kK][kK][A-Za-z0-9-]*

参见regex proof

REEX101解释

1st Alternative \d+(?:\d+(?:-?\d+){3}|\/\d+)
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Non-capturing group (?:\d+(?:-?\d+){3}|\/\d+)
1st Alternative \d+(?:-?\d+){3}
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Non-capturing group (?:-?\d+){3}
{3} matches the previous token exactly 3 times
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Alternative \/\d+
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Alternative \b[bB][kK][kK][A-Za-z0-9-]*
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
Match a single character present in the list below [bB]
bB matches a single character in the list bB (case sensitive)
Match a single character present in the list below [kK]
kK matches a single character in the list kK (case sensitive)
Match a single character present in the list below [kK]
kK matches a single character in the list kK (case sensitive)
Match a single character present in the list below [A-Za-z0-9-]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)

相关问题