在 PCRE 中,是否存在“否定交替"或“否定字符串类",即“否定字符类"之类的东西? [英] Is there a such thing as "negated alternation", or "negated string classes", a la "negated character classes", in PCRE?
问题描述
好的,所以当模式 [abc]
匹配单个字符 a
、b
或 c
,模式 [^abc]
匹配任何给定的字符,不是 a
、b
,或者c
.
Alright, so while the pattern [abc]
matches a single character that is a
, b
, or c
, the pattern [^abc]
matches any given character that isn't a
, b
, or c
.
现在,如果我在上面的语句中用 string 替换 character,如果我还将第一个模式更改为替代结构,如 (abc|def|ghi)
.-- 它将匹配三个字符的连续字符串,等于 abc
、def
或 ghi
.我该怎么做才能匹配不等于abc
、def
或ghi
的任何三个字符的字符串>?
Now if I substitue string for character in the above statement, it would still apply if I also changed the first pattern to an alternation construct like (abc|def|ghi)
. -- it would match a consecutive string of three characters equal to abc
, def
, or ghi
. What can I do to match any string of three characters that isn't equal to abc
, def
, or ghi
?
推荐答案
这是正则表达式的常见问题,简短的回答是否",正则表达式的运行方式并没有提供任何 3- 除...之外的字母字符串.
This is a common question with regexes, and the short answer is "no", the way regular expressions operate doesn't provide a way of saying "any 3-letter string except ...".
然而,一些正则表达式引擎允许所谓的否定前瞻断言".这些有点难以理解 - 这种环视解释似乎涵盖了它很好,如果从技术上讲一点.
However, some regular expression engines allow for what are called "negative look-ahead assertions". These are a little tricky to get your head around - this explanation of lookaround seems to cover it quite well, if a little technically.
前瞻的重要之处在于它是零宽度"——它不会用完"任何字符串.所以在 Javascript 中你可以匹配这样的字符串: 'abc123ghi'.match(/abc(?!def)...ghi/)
- (?!def)
表示在 'abc'
之后不能有字母 'def'
,但是 ...
需要在那里说那里应该有其他 3 个字符.
The important thing about lookahead is that it is "zero-width" - it doesn't "use up" any of the string. So in Javascript you can match a string like this: 'abc123ghi'.match(/abc(?!def)...ghi/)
- the (?!def)
says that there mustn't be the letters 'def'
after the 'abc'
, but the ...
needs to be there to say that there should be some other 3 characters there instead.
这篇关于在 PCRE 中,是否存在“否定交替"或“否定字符串类",即“否定字符类"之类的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!