![](/img/trans.png)
[英]Regex to match a word beginning with a period and ending with an underscore?
[英]Regex to match a string ending with a certain character but not beginning with another with intermediary whitespace
我正尝试使用正则表达式: (?<!:)[\\s]+"
无效。
我想要的是将引号与空格匹配,除非空格以冒号开头。
上面的正则表达式是无用的,因为它匹配不正确。 上面的正则表达式将匹配字符串:__"
(使用_
表示空格),因为它仅匹配_"
。 它从第二个空格开始匹配,但根本不匹配。
我在找:
A " - MATCH
B " - MATCH
: " - NO MATCH
A: " - NO MATCH
: " - NO MATCH
: " - NO MATCH
: A " - MATCH
负向后看无济于事,因为它确实与大多数匹配。
匹配所需字符串的一种方法是要求在\\s+
前面出现空格\\s
或冒号:
以外的其他字符:
(?<![:\s])\s+"
包括空格\\s
中的负向后看列表确保了空间不能算作匹配字符串的目的“不是一个冒号”字符。
尝试这个
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.