我有一个正则表达式的问题,我希望它能匹配字符串"This is a caterpillar s tooth"中的caterpillar,但它却匹配了cat。我该如何修改它?
List<string> women = new List<string>()
{
"cat","caterpillar","tooth"
};
Regex rgx = new Regex(string.Join("|",women.ToArray()));
MatchCollection mCol = rgx.Matches("This is a caterpillar s tooth");
foreach (Match m in mCol)
{
//Displays 'cat' and 'tooth' - instead of 'caterpillar' and 'tooth'
Console.WriteLine(m);
}
Regex.Escape
呢?只用\b
不就可以了吗? - Grant Smith\
字符,该怎么办? - SLaks\b(abc|def)\b
还是\b(abc|def\b)
- 您的C#示例给出了与第一行示例不同的结果。 - Gerwald