C# Regex to match the word with dot

52,350

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);
            }
        }
Share:
52,350
pili
Author by

pili

Updated on April 18, 2020

Comments

  • pili
    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
    manojlds about 13 years
    Usually Regular expression strings are better off being verbatim
  • manojlds
    manojlds about 13 years
    Same answers in gap of seconds :)
  • Waihon Yew
    Waihon Yew about 13 years
    @manojlds: I hope you agree that this is a matter of preference.
  • manojlds
    manojlds about 13 years
    Yes, but already complex regular expressions would have \\ strewn all around.
  • pili
    pili about 13 years
    Hi Harpyon, no results returned for this expression. If I just put "alphabet" there are 4 instances. Is there any specific syntax to c#?
  • pili
    pili about 13 years
    Hi 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
    Håvard about 13 years
    Are 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
    pili about 13 years
    Thanks guys but there is no results returned for the expression. Is there a c# specific expression like "^ $" ?
  • manojlds
    manojlds about 13 years
    Just verified in C#. Gives three alphabet..Please verify your code. See my edit
  • pili
    pili about 13 years
    Hi Harypyon, C# wanted to have this option and it worked.. Thanks... RegexOptions myRegexOptions = RegexOptions.None; Regex myRegex = new Regex(strRegex, myRegexOptions);
  • manojlds
    manojlds about 13 years
    @user712307 - ok your comments makes it a little more clear. How are you initializing entireText?
  • pili
    pili about 13 years
    @manojlds : RegexOptions myRegexOptions = RegexOptions.None; Regex myRegex = new Regex(strRegex, myRegexOptions); Did that and worked.. Thanks for your comments.
  • pili
    pili about 13 years
    HI Manjojlds.. thanks for your code. it works.. I added this portion: RegexOptions myRegexOptions = RegexOptions.None; Regex myRegex = new Regex(strRegex, myRegexOptions);
  • pili
    pili about 13 years
    If I want to get that alphabet if only it proceeds with " " or "\n" how should ammend that please..
  • pili
    pili about 13 years
    Hi Harypyon, If I want to get that alphabet if only it proceeds with " " or "\n" how should ammend that please..
  • manojlds
    manojlds about 13 years
    Use something like \salphabet\. for the regex. \s matches any whitespace character (spaces, tabs, line breaks).
  • Håvard
    Håvard about 13 years
    If you want to get alphabet only followed by a space or newline, you can use a lookahead: alphabet(?= |\n).
  • Hi-Angel
    Hi-Angel over 9 years
    It wasn't obvious, I'd underlined it: to make an expression work one needs to add the «@» sign before the string.