Suraj
Suraj

Reputation: 36547

help fixing up a regex - \r\n isn't being detected

Folks,

I have method that returns true if all characters are "legal" and false if a single character is "illegal". The definition is below (legal = letters, numbers, and some characters like $, - , %, etc). I want a newline and/or carriage return character to be "illegal". However, the method below thinks that its legal. How can I fix-up?

  private static bool NameHasAllLegalCharacters( string name )
    {
        var regexAlphaNum = new Regex( @"[^a-zA-Z0-9#\$%\^&\*\(\)\._\-\+=\[\]/<>{}:\s]" );
        return !regexAlphaNum.IsMatch( name );
    }

Upvotes: 0

Views: 106

Answers (4)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234474

\s matches this character set: [\f\n\r\t\v\x85\p{Z}]. You could simply enumerate this without \n and \r, like this:

@"[^a-zA-Z0-9#\$%\^&\*\(\)\._\-\+=\[\]/<>{}:\f\t\v\x85\p{Z}]"

I know it's ugly, but it should work.

Upvotes: 1

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146450

Given that there're thousands of characters out there (thanks to Unicode) I would not even try to use a blacklist approach. Instead, reverse your logic and test whether all chars belong to your whitelist:

private static bool NameHasAllLegalCharacters( string name )
{
    var regexAlphaNum = new Regex( @"^[a-zA-Z0-9#$%........]*$" );
    return regexAlphaNum.IsMatch( name );
}

Upvotes: 1

LukeH
LukeH

Reputation: 269368

The \s class matches all whitespace, and \r and \n are classed as whitespace.

Your regex will need to be more specific about which types of whitespace are allowed.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

\s matches all whitespace, not just the ASCII space.

Upvotes: 2

Related Questions