[英]Regular expression matching all text without part
I am trying to write regular expression which will match all lines ending with: XYZ
except lines which ending with AAXYZ
. 我正在尝试编写正则表达式,它将匹配以: XYZ
结尾的所有行,除了以AAXYZ
结尾的AAXYZ
。
Some examples: 一些例子:
ABCXYZ
- should be matched ABCXYZ
- 应该匹配 AAXYZ
- should NOT be matched AAXYZ
- 不应该匹配 ABXYZ
- should be matched ABXYZ
- 应该匹配 I started with following expression .*[^A][^A]XYZ$
but it does not work for example 3 我开始使用以下表达式.*[^A][^A]XYZ$
但它不适用于示例3
String[] lines=Regex.Split(input,"[\r\n]+")
.Where(x=>Regex.IsMatch(x,"^(?=.*(?<!AA)XYZ$).*$"))
.Select(x=>x.Value)
.ToArray();
If you need only regex 如果你只需要正则表达式
^(?=.*(?<!AA)XYZ$).*$
A regex seems like overkill here: 一个正则表达式似乎有点矫枉过正:
var matches = myInput.Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries)
.Where(x => x.EndsWith("XYZ") && !x.EndsWith("AAXYZ"))
.ToList();
If you need a regex, you could do something like: 如果您需要正则表达式,您可以执行以下操作:
^(|.|.*[^A].|.+[^A])XYZ$
This simply expands all the possibilities in a pipe-deliminated alternation: 这简单地扩展了管道划分交替的所有可能性:
XYZ
简单的XYZ
XYZ
(eg AXYZ
) 任何单个字符后跟XYZ
(例如AXYZ
) XYZ
(eg QAXYZ
, foobarBAXYZ
) 零个或多个字符后跟非A和另外一个字符,然后是XYZ
(例如QAXYZ
, foobarBAXYZ
) XYZ
(eg XBXYZ
, foobarXYZ
) 一个或多个字符后跟非A和XYZ
(例如XBXYZ
, foobarXYZ
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.