Character Sets

Character sets act as wildcards for a particular set of characters.

ExpressionMeaningInverse ("not")
[abcdef]Matches a single character in the list[^abcdef]
[a-f]Matches a single character in a range[^a-f]
\dMatches a decimal digit Same as [0–9]\D
\wMatches a word character\W
\sMatches a whitespace character Same as [\n\r\t\f]\S
\p{category}Matches a character in a specified category\P
.(Default mode) Matches any character except \n\n
.(SingleLine mode) Matches any character\n

Character categories

QuantifierMeaning
\p{L}Letters
\p{Lu}Uppercase letters
\p{Ll}Lowercase letters
\p{N}Numbers
\p{P}Punctuation
\p{M}Diacritic marks
\p{S}Symbols
\p{Z}Separators
\p{C}Control characters

To match exactly one of a set of characters, put the character set in square brackets:


using System;
using System.Text.RegularExpressions;


class Program
{
    static void Main(string[] args)
    {
        Console.Write(Regex.Matches("That demo is that demo.", "[Tt]hat").Count);  
    }
}

The output:


2

To match any character except those in a set, put the set in square brackets with a ^ symbol before the first character:


using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        Console.Write(Regex.Match("quiz quest", "q[^aeiou]").Index);
    }
}

The output:


0

You can specify a range of characters with a hyphen.

The following regular expression captures a chess move:


using System;
using System.Text.RegularExpressions;


class Program
{
    static void Main(string[] args)
    {
        Console.Write(Regex.Match("b1-c4", @"[a-h]\d-[a-h]\d").Success);
    }
}

The output:


True

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        Console.Write(Regex.IsMatch("Yes, please", @"\p{P}"));
    }
}

The output:


True

We will find more uses for \d, \w, and . when we combine them with quantifiers.

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.