繁体   English   中英

C#正则表达式将单词与点匹配

[英]C# Regex to match the word with dot

一只快速的棕狐狸跳过懒惰的狗”是英语的英文字母pangram,即包含所有字母的短语。它已用于测试打字机的字母,计算机键盘以及其他涉及英文字母中所有字母的应用程序。

我需要获取“字母”。 正则表达式中的单词。 在上面的文本中,有3个实例。 它不应包含“字母!”。 我刚刚尝试过正则表达式

 MatchCollection match = Regex.Matches(entireText, "alphabet."); 

但这会返回4个实例,包括“字母!”。 如何忽略这一点而仅获得“字母”。

. 是正则表达式中的特殊字符,可以匹配任何内容。 尝试转义:

 MatchCollection match = Regex.Matches(entireText, @"alphabet\.");

. 是正则表达式中的特殊字符。 您需要先用斜杠将其转义:

Regex.Matches(entireText, "alphabet\\.")

斜杠最终变成双斜杠,因为字符串中的\\必须反过来用另一个斜杠转义。

“。” 在正则表达式中有特殊含义。 逃脱以匹配时期

MatchCollection match = Regex.Matches(entireText, @"alphabet\.");

编辑:

完整代码,给出预期结果:

        string entireText = @"The quick brown fox jumps over the lazy dog is an English-language pangram, alphabet! that is, a phrase that contains all of the letters of the alphabet. It has been used to test typewriters alphabet. and computer keyboards, and in other applications involving all of the letters in the English alphabet.";
        MatchCollection matches = Regex.Matches(entireText, @"alphabet\.");
        foreach (Match match in matches)
        {
            foreach (Group group in match.Groups)
            {
                Console.WriteLine(group);
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM