0

我只想检测以下字符串:

pets ok
pets yes
pets allowed
pets accepted
pets is/are allowed
pets is/are accepted
pets will be considered
pets will considered
pets is/are considered
pet friendly

反过来也是一样的:

ok pets
yes pets
...

(同上,只是“pets”/“pet”从头到尾)

我想不通。

请,如果你能帮助我改进我目前显然不起作用的模式:

/\\b(pet(?:s)?\\s[negotiable|ok|o\\.k|friendly|accept|allow|possible]+|[ok|yes|accept]+\\spet(?:s)?|pet(?:s)?\\s[will|will\\sbe]+\\s[consider|allow|accept|possible]+)\\b/i"
4

2 回答 2

2

尝试

"/\\bpets?\\s[ok|is\\/are allowed]/i" 

和类似的

于 2012-11-02T12:23:57.840 回答
0

这是一个超严格的正则表达式,它只匹配您列出的示例(并且没有完成列出):

\b(?:(?:pets (?:ok|yes|(?:(?:is\/are )?(?:allowed|accepted))|(?:(?:will(?: be)?|is\/are) considered)))|(?:(?:ok|yes|(?:(?:is\/are )?(?:allowed|accepted))|(?:(?:will(?: be)?|is\/are) considered))) pets|pet friendly|friendly pet)\b​

在 Rubular 上查看

我基本上将其分为“允许/接受”的变体和“考虑”的变体。然后我只是以相反的方式复制它。

最人类可读的形式仍然是:

\b(?:pets ok|pets yes|pets allowed|pets accepted|pets is\/are allowed|pets is\/are accepted|pets will be considered|pets will considered|pets is\/are considered|pet friendly|ok pets|yes pets|allowed pets|is\/are allowed pets|accepted pets|is\/are accepted pets|will be considered pets|will considered pets|is\/are considered pets|friendly pet)\b

此外,在您的正则表达式中,您似乎使用字符类 ( [ ]) 来分隔单词,而实际上您应该使用非捕获组 ( (?: ))

于 2012-11-02T14:03:09.133 回答