Ashish K Agarwal
Ashish K Agarwal

Reputation: 1172

Issue with regex in ASP.net for german, french & spanish languages

I want to support German, French & Spanish characters on a particular field of my website. I need a regex for this. Presently I am using -

^[\w\s-\+\$\*\.\?\:\;\!\,"'\%\&\/\(\)\@\#«»£°¿¡_ÀÂÆÇÈÉÊËÎÏÔŒÙÛÜàâæçèéêëîïôœùûüÄÖäößÁÍÑÓÚáíñóú\u201E\u201C\u201D\u20AC]{1,255}$

This regex basically uses all the char set from the 3 languages I mentioned.

Is there a neat way to avoid this lengthy regex? I tried /p{L}/p{Z} regex. However this didnt work.

My website is in ASP.net

Upvotes: 0

Views: 242

Answers (1)

stema
stema

Reputation: 92986

  1. /p{L}/p{Z} is wrong, should be \p{L}\{Z}.

  2. all the letters, like "ÀÂÆÇÈ" shouldn't be needed, they are all included in \w in .net!

  3. You don't need most of the escaping in a character class

  4. You can't write something like " in a character class, only thing what happens is that every single character is added to the class.

This should be quite similar to what you used:

^[-\p{L}\p{N}\p{P}\p{Z}_+$*%&/@#«»£°\u201E\u201C\u201D\u20AC]{1,255}$

I haven't checked those Unicode codepoints at the end of the class, I don't now if they are needed or not.

For an explanation of all the \p{...} items see Unicode Regular Expressions on regular-expressions.info

Upvotes: 1

Related Questions