Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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?

like image 560
user1322801 Avatar asked Oct 28 '12 09:10

user1322801


People also ask

What does * do in regex?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.

How do I find a star in regex?

* in Regex means: Matches the previous element zero or more times. so that, you need to use \* or [*] instead. When followed by a character that is not recognized as an escaped character in this and other tables in this topic, matches that character.

How do I find a character in a string in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What does ?= * Mean in regex?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).


2 Answers

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

like image 76
SLaks Avatar answered Oct 05 '22 23:10

SLaks


* 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.

like image 22
Ria Avatar answered Oct 06 '22 01:10

Ria