C# Regex to match the word with dot
Solution 1
.
is a special character in regex, that matches anything. Try escaping it:
MatchCollection match = Regex.Matches(entireText, @"alphabet\.");
Solution 2
.
is a special character in regular expressions. You need to escape it with a slash first:
Regex.Matches(entireText, "alphabet\\.")
The slash ends up being double because \
inside a string must in turn be escaped with another slash.
Solution 3
"." has special meaning in Regular expressions. Escape it to match the period
MatchCollection match = Regex.Matches(entireText, @"alphabet\.");
Edit:
Full code, giving expected result:
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);
}
}
pili
Updated on April 18, 2020Comments
-
pili about 4 years
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.
I need to get the "alphabet." word in regex. In the above text there are 3 instances. It should not include "alphabet!". I just tried regex with
MatchCollection match = Regex.Matches(entireText, "alphabet.");
but this returns 4 instances including "alphabet!". How to omit this and get only "alphabet."
-
manojlds about 13 yearsUsually Regular expression strings are better off being verbatim
-
manojlds about 13 yearsSame answers in gap of seconds :)
-
Waihon Yew about 13 years@manojlds: I hope you agree that this is a matter of preference.
-
manojlds about 13 yearsYes, but already complex regular expressions would have \\ strewn all around.
-
pili about 13 yearsHi Harpyon, no results returned for this expression. If I just put "alphabet" there are 4 instances. Is there any specific syntax to c#?
-
pili about 13 yearsHi manojlds, no results returned for this expression. If I just put "alphabet" there are 4 instances. Is there any specific syntax to c#?
-
Håvard about 13 yearsAre you sure it doesn't work? I'm unable to test the C# portion of it, but the regex seems to be working when I test it on RegexHero.
-
pili about 13 yearsThanks guys but there is no results returned for the expression. Is there a c# specific expression like "^ $" ?
-
manojlds about 13 yearsJust verified in C#. Gives three alphabet..Please verify your code. See my edit
-
pili about 13 yearsHi Harypyon, C# wanted to have this option and it worked.. Thanks... RegexOptions myRegexOptions = RegexOptions.None; Regex myRegex = new Regex(strRegex, myRegexOptions);
-
manojlds about 13 years@user712307 - ok your comments makes it a little more clear. How are you initializing entireText?
-
pili about 13 years@manojlds : RegexOptions myRegexOptions = RegexOptions.None; Regex myRegex = new Regex(strRegex, myRegexOptions); Did that and worked.. Thanks for your comments.
-
pili about 13 yearsHI Manjojlds.. thanks for your code. it works.. I added this portion: RegexOptions myRegexOptions = RegexOptions.None; Regex myRegex = new Regex(strRegex, myRegexOptions);
-
pili about 13 yearsIf I want to get that alphabet if only it proceeds with " " or "\n" how should ammend that please..
-
pili about 13 yearsHi Harypyon, If I want to get that alphabet if only it proceeds with " " or "\n" how should ammend that please..
-
manojlds about 13 yearsUse something like \salphabet\. for the regex. \s matches any whitespace character (spaces, tabs, line breaks).
-
Håvard about 13 yearsIf you want to get
alphabet
only followed by a space or newline, you can use a lookahead:alphabet(?= |\n)
. -
Hi-Angel over 9 yearsIt wasn't obvious, I'd underlined it: to make an expression work one needs to add the «@» sign before the string.