C# Regex to match the word with dot

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."

Answers


. is a special character in regex, that matches anything. Try escaping it:

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

. 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.


"." 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);
            }
        }

Need Your Help

Django Model Field Default to Null

python django django-models

I need to have my Django application allow me to have a default value of NULL set for a certain model field. I've looked over the null, blank, and default parameters, but it's not very clear what

How to automatically change the text size inside a div?

jquery html css font-size

I have a background image with a div. I want to write some text, but this text should change the font size through the div.