Max
Max

Reputation: 1299

C# Regular Expression always returns FALSE

regexPattern="\w{6}(AAAAA|BBBBB|CCCCC)"  

I need the strings below to return TRUE. So ANY 6 letters followed by AAAAA or BBBBB or CCCCC:

TXCDTLAAAAA000
TXCDTLBBBBB111
TXCDTLCCCCC222

but giving the pattern above I always get a FALSE in return. How do I fix this pattern to work right?

So Basically this code is working:

    if (Regex.IsMatch("123456BBBBB", @"\w{6}(AAAAA|BBBBB|CCCCC)"))
    {
        //true
    }

so I am fixing the code now Thank you!

Upvotes: 0

Views: 875

Answers (1)

mrjoltcola
mrjoltcola

Reputation: 20842

You didn't mention which host language you are using, but the backslash is usually an escape character in double quoted string, so if it is a common language, you may need double backslash

 regexPattern="\\w{6}(AAAAA|BBBBB|CCCCC)" 

Or use another way to express the pattern that doesn't require escape characters. For example, in Python you can prefix the raw string:

 regexPattern = r"\w{6}(AAAAA|BBBBB|CCCCC)"

Although Python won't treat the \w as an escape sequence anyway, but it will help for others.

With C# use @ (verbatim string) to accomplish it:

 var regexPattern = @"\w{6}(AAAAA|BBBBB|CCCCC)";

Upvotes: 3

Related Questions