有没有“更好”的方法?进行条件环视的方法?

发布于 2025-01-17 02:02:58 字数 953 浏览 4 评论 0原文

<代码>(?=(模式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

我有更复杂的模式,所以这个方法将继续工作,但是我错过了一些东西还是有新的正则表达式方法?

regex101 示例

(?=(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??

regex101 example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2025-01-24 02:02:58

您可以编写不带前瞻的模式,并且仅当存在左括号时才使用单个条件来匹配右括号。

匹配数字 83 和 84 可以使用字符类 8[34] 进行缩短,并且 s|'s 可以缩短为 '?s

(?i)(?<!\S)(?:(\()?\b(?:red|gold|silver|king) pack(?(1)\))(?: (?:70|8[34]|1[02]0)'?s)?|(?:\b(?:red|gold|silver|king) )?\b(?:70|8[34]|1[02]0) ?(?:'s)?) box\b

模式匹配:

  • (?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] and s|'s can be shortened to '?s

(?i)(?<!\S)(?:(\()?\b(?:red|gold|silver|king) pack(?(1)\))(?: (?:70|8[34]|1[02]0)'?s)?|(?:\b(?:red|gold|silver|king) )?\b(?:70|8[34]|1[02]0) ?(?:'s)?) box\b

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 and pack
    • (?(1)\)) Conditional, match ) if capture group 1 exists
    • (?: (?:70|8[34]|1[02]0)'?s)? Optionally match a space, any of the alternatives, optional ' and s
    • | 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 group
  • box\b Match box followed by a word boundary

See a regex demo.

完美的未来在梦里 2025-01-24 02:02:58

看吧:

(?: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文