23

Allowed characters are (at least) A-Z, a-z, 0-9, ö, Ö, ä, ä, å, Å and german, latvian, estonian (if any) special chars? Is there ready-made method or do i have to make blacklist (non-allowed chars) and regular expressions IsMatch? If no ready-made how to use blacklist?

2

4 Answers 4

34

I don't know how special characters from all those languages are categorised, but you could check if the Char.IsLetterOrDigit method matches what you want to do. It works at least for the digits and letters I tested:

string test = "Aasdf345ÅÄÖåäöéÉóÓüÜïÏôÔ"; if (test.All(Char.IsLetterOrDigit)) { ... } 

The Char.IsLetterOrDigit returns true for characters that are categorised in Unicode as UppercaseLetter, LowercaseLetter, TitlecaseLetter, ModifierLetter, OtherLetter, or DecimalDigitNumber.

Sign up to request clarification or add additional context in comments.

3 Comments

What's test.All? That's not a string method, is it some kind of extension method? Or a LINQ method?
@Task All is a linq extension of string. See msdn.microsoft.com/en-us/library/system.string.aspx
Ah! The "Extension Methods" section of the documentation is new to me, I hadn't seen that before. I guess I've gotten too used to finding everything I need in the earlier "Properties" or "Methods" area. Thanks!
13

Investigate char.IsLetterOrDigit(char).

For example:

myString.All(c => char.IsLetterOrDigit(c)); 

3 Comments

Just curious, but why was this downvoted? As far as I can tell it's a perfectly valid way of doing what the OP asked.
Ah.. just had a closer look; never noticed the 0-9 requirement in there. I've amended my answer to use IsLetterOrDigit instead of just IsLetter.
A shorthand for this is myString.All(char.IsLetterOrDigit);
4

A blacklist for characters is likely pretty large :-)

You can use the regular expression

^[\d\p{L}]+$ 

to match decimal digits and letters, regardless of script.

This regular expression consists of a character class containing the shorthands \d – which contains every digit (230 in total in the BMP) and \p{L} which contains every Unicode character classified as a "letter" (46817 in the BMP). Said character class is then repeated at least once and embedded between ^ and $ – the string start and end anchors, so it matches the complete string.

For some regex engines, since you're only interested in Latin letters, apparently, you could also use

^[\d\p{Letter}]+$ 

However, .NET doesn't support this. The first regex mentioned above actually catches everything that's a digit or a letter in any script. So it will dutifully match on Indian or Arabic numerals and Hebrew, Cyrillic and other non-Latin scripts. Depending on what you want this may not be appropriate.

If that poses a problem, then I see no better option than to explicitly list the characters you want to allow. However, I consider it dangerous to assume that text in a certain language is always restricted to that language's script. If I were to write a Czech or Polish name in a German text, then I'd likely need more than just [a-zA-ZäöüÄÖÜß].

1 Comment

thanks! can you please explain how ^[\d\p{L}]+$ works. I checked from the web but I couldn't sum it up entirely...
0

It would be simpler to match allowed characters catch a false return.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.