user1322801
user1322801

Reputation: 859

Star - look for the character * in a string using regex

I am trying to find the following text in my string : '***' the thing is that the C# Regex mechanism doesnt allow me to do the following:

new Regex("***", RegexOptions.CultureInvariant | RegexOptions.Compiled);

due to

ArgumentException: "parsing "*" - Quantifier {x,y} following nothing."

obviously it thinks that my stars represents regular expressions, is there a way to tell the Regex mechanism to treat stars as just stars and nothing else?

Upvotes: 12

Views: 38353

Answers (2)

Ria
Ria

Reputation: 10347

* in Regex means:

Matches the previous element zero or more times.

so that, you need to use \* or [*] instead.

explain:

\

When followed by a character that is not recognized as an escaped character in this and other tables in this topic, matches that character. For example, \* is the same as \x2A.

[ character_group ]

Matches any single character in character_group.

Upvotes: 21

SLaks
SLaks

Reputation: 887469

You need to escape the star with a backslash: @"\*"

Upvotes: 17

Related Questions