Reputation: 1190
new Regex(@"\n|\r|\\|<|>|\*|!|\$|%|;");
I have an regex example above, but I can not really understand what is trying to find? can anyone give me a hand please?
Upvotes: 0
Views: 86
Reputation: 336128
The regex matches one of the characters separated by the alternation operator |
. There are a few special characters (like \n
or \r
for newline and carriage return, or \$
for a literal dollar sign and \*
for a literal asterisk because $
and *
are regex metacharacters), but other than that, it's quite straightforward.
That said, for matching a single character out of a list of valid characters, a character class is usually the better choice, not only because there is less need to escape the metacharacters:
new Regex(@"[\n\r\\<>*!$%;]");
Upvotes: 4
Reputation: 213223
|
in regex is an alternation operator. A|B
means match either A
or B
. It can also be written using a character class - [AB]
which also means the same thing.
The benefit of using character class is, you don't need to escape regex meta-characters inside it, which you have to do outside, as you did for *
. So, your regex can be shortened to:
new Regex(@"[\n\r\\<>*!$%;]");
Upvotes: 2
Reputation: 16232
It'll try to match any of the special character listed: \n
, \r
, \
, <
, >
, *
, !
, $
, %
The |
is the regex OR operator.
Some characters need to be escaped with an extra \
as they have a signification in the regex lanugage (\
, $
, ...)
Upvotes: 2