正则表达式匹配括号(Regex match parenthesis)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
正则表达式匹配括号(Regex match parenthesis)

我有以下字符串:

The service(s), foo bar

我有一个正则表达式,检查字符串中是否包含“The”和“service(s)”字样。 我应该用反斜杠转义括号,但它似乎不起作用。 我必须使用这个正则表达式的逻辑结构。 你能帮我解决逃跑问题吗?

不工作:

(^(?=.*\b(?i)The(?-i)\b)(?=.*\b(?i)service\(s\)(?-i)\b).*$)

目标语言是C#。

I have the following string:

The service(s), foo bar

I have a regex that checks if the words 'The' and 'service(s)' are contained within the string. I'm supposed to escape the brackets with backslash, but it does not seem to work. I must use the logical structure of this Regex. Can you help me fix the escaping?

Not working:

(^(?=.*\b(?i)The(?-i)\b)(?=.*\b(?i)service\(s\)(?-i)\b).*$)

Target language is C#.

最满意答案

service(s)结束括号断言下一个立即位置是非单词边界。 所以你不需要last \b标记,因为你可能意味着一个非字边界\B 我认为您不需要设置和取消设置不区分大小写的修饰符:

(?i)(?=.*\bThe\b)(?=.*\bservice\(s\)\B).*$

Closing parenthesis in service(s) asserts that next immediate position is a non-word boundary. So you don't need last \b token since you possibly meant a non-word boundary \B. I don't think you need to set and unset case-insensitive modifier either:

(?i)(?=.*\bThe\b)(?=.*\bservice\(s\)\B).*$

更多推荐

本文发布于:2023-04-13 12:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/60d0fc431a82dbdf6017495bb5234eb1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:括号   正则表达式   Regex   match   parenthesis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!