1

我有许多与产品相关的字符串。这些中的每一个都有参考号,我想创建一个正则表达式,如果不止一次提到不同的参考号,它会拾取。因此,给出以下示例:

"AB01 MyProduct" >>> No match - because there is only one ID
"AB02 MyOtherProduct" >>> No match - because there is only one ID
"AB03 YetAnotherProduct" >>> No match - because there is only one ID
"AnAccessory for AB01, AB02, AB03 or AB101" >>> Matches!!
"AB02 MyOtherProduct, MyOtherProduct called the AB02" >>> No match - because the codes are the same

谁能给我一个线索?

4

1 回答 1

2

如果您的正则表达式引擎支持负前瞻,这可以解决问题:

(AB\d+).*?(?!\1)AB\d+

如果有两个序列匹配AB\d+并且第二个序列与第一个序列不同(由负前瞻确保),则它匹配。

解释:

(           # start capture group 1
 AB         # match `AB` literally
 \d+        # match one or more digits
)           # end capture group one
.*?         # match any sequence of characters, non-greedy
(?!         # start negative lookahead, match this position if it does not match
 \1         # whatever was captured in capture group 1
)           # end lookahead
AB          # match `AB` literally
\d+         # match one or more digits

测试(JavaScript):

> var pattern = /(AB\d+).*?(?!\1)AB\d+/;
> pattern.test("AB01 MyProduct")
  false
> pattern.test("AnAccessory for AB01, AB02, AB03 or AB101")
  true
> pattern.test("AB02 MyOtherProduct, MyOtherProduct called the AB02")
  false
于 2013-01-10T12:38:38.920 回答