C#RegEx同时匹配字符串的开头和单词的开头(C# RegEx match beginning of string and beginning of word simultaneously)

编程入门 行业动态 更新时间:2024-10-28 19:33:00
C#RegEx同时匹配字符串的开头和单词的开头(C# RegEx match beginning of string and beginning of word simultaneously)

假设有六个字符串

“abcd bbb ccc” “abce bbb ccc”

“abcf bbb ccc”

“aaa abcd ccc”

“aaa abce ccc” “aaa abcf ccc”

用户输入表达式“ <abc[!e] ”

此表达式转换为以下正则表达式“^ abc [^ e]”

翻译的正则表达式将成功匹配字符串1和3

如果我想匹配字符串4和6,表达式必须翻译成“\ Wabc [^ e]”(这也会匹配abc字符之前的空格:(,这不好)

不幸的是,用户输入的表达式需要在正则表达式中进行转换,以匹配1,3和4,6个字符串。

有没有办法将用户输入的表达式转换为正则表达式组合“^ abc [^ e]”和“\ Wabc [^ e]”表达式(理想情况下,第二个表达式与第一个空格不匹配:))

Say there are six strings

"abcd bbb ccc" "abce bbb ccc"

"abcf bbb ccc"

"aaa abcd ccc"

"aaa abce ccc" "aaa abcf ccc"

User enters expression "<abc[!e]"

This expression gets translated into following regex "^abc[^e]"

Translated regex would successfully match strings 1 and 3

If I wanted to match strings 4 and 6 the expression has to be translated into "\Wabc[^e]" (this would also match space before abc chars :(, which is not good)

Unfortunately user entered expression needs to be converted in regex that would match both 1,3 and 4,6 strings.

Is there way to translate user entered expression into regex that would combine "^abc[^e]" and "\Wabc[^e]" expressions (ideally second expression would not match first space :) )

最满意答案

Perl知道零宽度字边界\ b:

\babc[^e]

我认为它应该适用于大多数Perl风格的正则表达式引擎。

Perl knows the zero-width word boundary \b:

\babc[^e]

I think that it should work in most Perl-style regular expression engines.

更多推荐

本文发布于:2023-07-05 01:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031313.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:开头   字符串   单词   RegEx   match

发布评论

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

>www.elefans.com

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