假设有六个字符串
“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.
更多推荐
发布评论