0

How to check a string that contains special characters such as _ . , ( ) & ^ % $ # @ ! * in my string? I tried to create a regular expression validator for checking the string as follows.

bool isOk = Regex::IsMatch(firstname, "^[a-zA-Z]+$");

This is the name field in registration form. It should allow Japanese characters. But with my RegEx only allow English characters. I know this is because [a-zA-Z]. But how to check only the special characters and leave Japanese or Korean characters?

Nelson T Joseph
  • 2,683
  • 8
  • 39
  • 56
  • 1
    This may be helpful http://www.regular-expressions.info/unicode.html – Rahul G Nair Jan 10 '14 at 06:38
  • possible duplicate of [Change RegEx to allow for both English & Japanese characters](http://stackoverflow.com/questions/15151230/change-regex-to-allow-for-both-english-japanese-characters) – brandonscript Jan 10 '14 at 07:10

2 Answers2

1

I have just found some examples of RegExp expressions for Unicode characters:

Regex for matching ALL Japanese common & uncommon Kanji

`(4e00 – 9fcf)` 

~ The Big Kahuna!

([一-龯])

Regex for matching Hirgana or Katakana

([ぁ-んァ-ン])

and many more...

regex-japanese.txt from terrancesnyder on GitHubGist: https://gist.github.com/terrancesnyder/1345094

One more good reply: Regular expressions (regex) in Japanese

Community
  • 1
  • 1
1

Since you probably have a better idea of what you don't want to allow than what you do want to allow, use an inverted set of characters using the [^ ] syntax.

bool isOk = Regex::IsMatch(firstname, "^[^_\\.,\\(\\)&^%$#@!\\*]+$");

But please note that in English 'FULL STOP' (U+002E) is sometimes used in names. Your blacklist approach would prohibit them. So, consider removing it from the code above.

Also, 'SPACE' (U+0020) and 'HYPHEN-MINUS' (U+002D) are used in names in English. Your whitelist approach would prohibit them.

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72