有没有“更好”的方法?进行条件环视的方法?
<代码>(?=(模式1|模式2)) (模式1)? (模式2)? pattern3|pattern4
这就是我最终得到的结果,并且它有效,但是有没有更简洁的方法? 我想找到所有包含“box”并具有一个或多个前缀的字符串:pack 和/或 length。
Marlboro 100's Box
Marlboro Gold Pack 100's Box
Marlboro Special Blend (Gold Pack) 100's Box
Marlboro Silver Pack Box
Marlboro Special Blend (Red Pack) 100s Box
Pall Mall RED 100 BOX
Marlboro Special Blend (Gold Pack) Box
(?i)(?=(((\()?(红|金|银|王)(包)?(\))?)|((70|83|84|100|120)( s|'s)?)))((((\()?(红|金|银|王)(包)?(\))? )?((70|83|84|100|120)(s|'s)?)?)(\bbox\b))
只是想读一下这篇文章就会让我的大脑流血。我可以在代码中拆分模式并轻松地重用它们,但是......我错过了什么吗?
pattern1 = ((\()?(red|gold|silver|king)( pack)?(\))?)
pattern2 = ((70|83|84|100|120)(s|'s)?)
(?=(pattern1|pattern2)) (pattern1)? (pattern2)? pattern3
我有更复杂的模式,所以这个方法将继续工作,但是我错过了一些东西还是有新的正则表达式方法?
(?=(pattern1|pattern2)) (pattern1)? (pattern2)? pattern3|pattern4
This is what I have ended up with, and it works, but is there a more parsimonious method?
I want to find all strings that contain "box" and have one or more prefixes: pack and/or length.
Marlboro 100's Box
Marlboro Gold Pack 100's Box
Marlboro Special Blend (Gold Pack) 100's Box
Marlboro Silver Pack Box
Marlboro Special Blend (Red Pack) 100s Box
Pall Mall RED 100 BOX
Marlboro Special Blend (Gold Pack) Box
(?i)(?=(((\()?(red|gold|silver|king)( pack)?(\))?)|((70|83|84|100|120)(s|'s)?)))((((\()?(red|gold|silver|king)( pack)?(\))? )?((70|83|84|100|120)(s|'s)? )?)(\bbox\b))
Just trying to read this causes my brain to bleed. I can split the patterns up in code and reuse them easily enough, but ... am I missing something??
pattern1 = ((\()?(red|gold|silver|king)( pack)?(\))?)
pattern2 = ((70|83|84|100|120)(s|'s)?)
(?=(pattern1|pattern2)) (pattern1)? (pattern2)? pattern3
I have more complex patterns, so this method will continue to work, but am I missing something or are there new regex methods??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以编写不带前瞻的模式,并且仅当存在左括号时才使用单个条件来匹配右括号。
匹配数字 83 和 84 可以使用字符类
8[34]
进行缩短,并且s|'s
可以缩短为'?s
模式匹配:
(?i)
不区分大小写匹配的内联修饰符(? 在左侧断言空白边界
(?:
非捕获组(\()?
可选捕获组 1,匹配(
\b(?:red|gold|silver|king) pack
单词边界,匹配任何替代项和pack
(?(1)\))
有条件,如果捕获组 1 存在,则匹配)
(?: (?:70|8[34]|1[02]0)'?s)?
可选择匹配空格、任何替代项、可选'< /code> 和
s
|
或者(?:\b(?:red|gold|silver|king) )?
可选择匹配单词边界以及任何后跟空格的替代项\b(?:70|8[34]|1[02]0) ?(?:'s)?
单词边界,匹配任何数字、可选空格和可选匹配的
)
关闭非捕获组box\b
匹配box
后跟单词边界查看 正则表达式演示。
You might write the pattern without a lookahead, and use a single conditional for matching a closing parenthesis only when there is an opening parenthesis.
Matching the number 83 and 84 could be shortened using a character class
8[34]
ands|'s
can be shortened to'?s
The pattern matches:
(?i)
Inline modifier for a case insensitive match(?<!\S)
Assert a whitespace boundary to the left(?:
Non capture group(\()?
Optional capture group 1, match(
\b(?:red|gold|silver|king) pack
A word boundary, match any of the alternatives andpack
(?(1)\))
Conditional, match)
if capture group 1 exists(?: (?:70|8[34]|1[02]0)'?s)?
Optionally match a space, any of the alternatives, optional'
ands
|
Or(?:\b(?:red|gold|silver|king) )?
Optionally match a word boundary and any of the alternatives followed by a space\b(?:70|8[34]|1[02]0) ?(?:'s)?
A word boundary, match any of the numbers, optional space and optionally match's
)
Close non capture groupbox\b
Matchbox
followed by a word boundarySee a regex demo.
看吧:
(?:gold|red|silver|king|pack|[() ])+(?:\d+)?(?:'|s| )+?box$
注意:这个具有捕获一个前导空格的副作用。
https://regex101.com/r/QKEX6f/1
Behold:
(?:gold|red|silver|king|pack|[() ])+(?:\d+)?(?:'|s| )+?box$
Note: this has a side effect of capturing one leading space.
https://regex101.com/r/QKEX6f/1