[英]Regex to match a string ending with a certain character but not beginning with another with intermediary whitespace
I am attempting to use the regex: (?<!:)[\\s]+"
to no effect. 我正尝试使用正则表达式:
(?<!:)[\\s]+"
无效。
What I want is to match a quote mark preceded by whitespace UNLESS the whitespace is preceded by a colon. 我想要的是将引号与空格匹配,除非空格以冒号开头。
The above regex is useless however as it matches incorrectly. 上面的正则表达式是无用的,因为它匹配不正确。 The regex above will match the string
:__"
(using _
to represent a space) because it just matches _"
. 上面的正则表达式将匹配字符串
:__"
(使用_
表示空格),因为它仅匹配_"
。 It starts matching at the second space, but it shouldn't match at all. 它从第二个空格开始匹配,但根本不匹配。
I'm looking for: 我在找:
A " - MATCH
B " - MATCH
: " - NO MATCH
A: " - NO MATCH
: " - NO MATCH
: " - NO MATCH
: A " - MATCH
The negative lookbehind doesn't help because it does match most of them. 负向后看无济于事,因为它确实与大多数匹配。
One way to match the strings that you want is to require a character other than a space \\s
or colon :
to be present in front of \\s+
: 匹配所需字符串的一种方法是要求在
\\s+
前面出现空格\\s
或冒号:
以外的其他字符:
(?<![:\s])\s+"
Including space \\s
in the list of negative look-behind ensures that a space cannot be counted as "not-a-colon" character for the purpose of matching a string. 包括空格
\\s
中的负向后看列表确保了空间不能算作匹配字符串的目的“不是一个冒号”字符。
Try this 尝试这个
const string FILENAME = @"\temp\test.txt";
static void Main(string[] args)
{
string input = File.ReadAllText(FILENAME);
string pattern = "^[^:]\\s+\"";
MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Multiline);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.