4

How would I write a regular expression (C#) which will check a given string to see if any of its characters are characters OTHER than the following:

a-z
A-Z
Æ æ Å å Ø ø - '

1
  • unlike JavaScript, C# does not have regular expressions as part of the language. You are asking about .NET regular expressions, not c# regular expressions. Commented May 29, 2010 at 14:49

2 Answers 2

13
new Regex("[^a-zA-ZÆæÅ娸'-]") 

The [] creates a character class, then ^ specifies negation, so a character matches the class if it's not one of those listed.

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

7 Comments

The ' should be excluded too.
Note that the - character is the last in the list. If you want more characters excluded don't add them after the -, put them before (or escape the - with backslash), otherwise the characters to the left and right of the - will be treated as a character range.
you can actually do "[^a-zA-ZÆ-Åæ-å'-]" to know that requires knowledge of the Danish alphabet of course :p
@Rune, not sure how serious you're being. That will actually throw an exception. The Unicode code point order does not match the Danish alphabet. Æ and Å are right next to each other in Unicode, but not in that order. That also means you're leaving out Ø and ø, which are later.
The order should be æ,ø,å that's the alphabetic order of those letters and that's the exact order in the ascii table (the Danish ASCII table).
|
1

You can use character grouping in combination with the negation operator to achieve this.

You also need to escape the - character (and potentially the ') using a \

Your final expression would read:

[^a-zA-ZÆæÅ娸\-\']*

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.