简体   繁体   中英

C# Regular Expression-Special characters

I have below mentioned regular expression which will identify the following special characters:

~!@©#$%^&*()_+{}|:"<>?`€[]\;',./

The regex:

var rx = new Regex(@"[\p{IsLatin-1Supplement}\p{P}\p{S}]");
var str = "~!@©#$%^&*()_+{}|:\"<>?€[]\\;',./`éöò";
var all = rx.Matches(str).Cast<Match>().ToList();

How can I exclude a literal dot character ( . ) in above regular expression as I need to validate a price value like " 16.01 " which is valid (dot is valid in this case).

\\p{P} contains the . symbol, thus, it is matched.

You can subtract the dot from the character class if it is no longer a "special" char for your case, and other punctuation symbols in your list are:

[\p{IsLatin-1Supplement}\p{P}\p{S}-[.]]

And then 16.01 will not match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM