[英]Don't match if there's a "period" between "word" and "?"
Perhaps he hadn't. [Perhaps he didn't?]
Perhaps he didn't.
[Perhaps he hadn't?]
I want to match the bits in brackets;我想匹配括号中的位; sentences that start with "Perhaps" and end with a question mark.
以“也许”开头并以问号结尾的句子。
I thought this regex would work: Perhaps.*(?!=\\.)\\?
我认为这个正则表达式会起作用:
Perhaps.*(?!=\\.)\\?
However, what I'm getting is this:但是,我得到的是:
[Perhaps he hadn't. Perhaps he didn't?]
Perhaps he didn't.
[Perhaps he hadn't?]
Why is this?为什么是这样? And how to fix it?
以及如何修复它?
You may use a negated character class like this:您可以使用这样的否定字符类:
/Perhaps[^.?]*\?/g
To match complete word use:要匹配完整的单词使用:
/\bPerhaps[^.?]*\?/
And to avoid matching across lines use:为了避免跨行匹配,请使用:
/\bPerhaps[^.?\r\n]*\?/
here [^.?]
would match any character except .
这里
[^.?]
将匹配除.
and ?
和
?
About your regex:关于你的正则表达式:
(?!=\\.)
is actually wrong syntax for a negative lookahead. (?!=\\.)
实际上是否定前瞻的错误语法。 It just means don't match if we have a literal =
and .
如果我们有文字
=
和 ,这只是意味着不匹配.
ahead.先。
Even if you correct it to use Perhaps.*(?!\\.)\\?
即使您更正它以使用
Perhaps.*(?!\\.)\\?
it will still not work because (?!\\.)
will only be applied for matching ?
它仍然不起作用,因为
(?!\\.)
将仅用于匹配?
and that will always succeed.这将永远成功。
Though not recommended but if you really want to use a negative lookahead then use:虽然不推荐,但如果你真的想使用负前瞻,那么使用:
/Perhaps(?:(?!\.).)*\?/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.