简体   繁体   中英

Special characters in regex

I have a pattern which looks like this (I'm not the creator):

[a-z,A-z,0-9,\-,\+,\&,\/,\\\,\s]{1,127}

which i pass to Regex.IsMatch() .

Is there a "better" way of writing the same expression? And by better I mean shorter.

And if I would like to add a special character like æ , do I simply add \\æ ?

You can start by removing the duplicates. This includes the repeated commas, as well as the az because this is encapsulated in the Az range, as is the \\ .

You also do not have to escape most characters, and you can pull the - to the front of the character class to avoid escaping that one too.

This leaves you with:

[-+&/,A-z0-9\s]{1,127}

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