nidarshani fernando
nidarshani fernando

Reputation: 533

Regular expression to match 0 or more occurrences not working

This question may sound stupid. But I have tried several options and none of them worked.

I have the following in a string variable.

string myText="*someText*someAnotherText*";

What I mean by above is that, there can be 0 or more characters before "someText". There can be 0 or more characters after "someText" and before "someAnotherText". Finally, there can be 0 or more occurrences of any character before "someAnotherText".

I tried the following.

string res= Regex.Replace(searchFor.ToLower(), "*", @"\S*");

It didn't work. Then I tried the following.

string res= Regex.Replace(searchFor.ToLower(), "*", @"\*");

Even that didn't work.

Can someone help pls ? Even though I have mentioned "*" to indicate 0 or more occurrences, it says that I haven't mentioned the number of occurrences.

Upvotes: 5

Views: 13327

Answers (2)

Suragch
Suragch

Reputation: 511696

The regular expression to match 0 or more occurrences of any character is

.*

where . matches any single character and * matches zero or more occurrences of it.

(This answer is a quick reference simplification of the current answer.)

Upvotes: 0

Corey
Corey

Reputation: 16564

Unlike the DOS wildcard character, the * character in a regular expression means repeat the previous item (character, group, whatever) 0 or more times. In your regular expression the first * has no preceding character, the second one follows the t character, so will repeat that any number of times.

To get '0 or more of any character' you need to use the composition .* where . is 'any character' and * is '0 or more times'.

In other words to search for someText followed any number of characters later by someAnotherText you would use the following Regex:

var re = new Regex(@"someText.*someAnotherText");

Note that unless you specify otherwise by putting start/end specifiers in (^ for start of string, $ for end) the Regex will match any substring of the test string.

Tests for the above, all returning true:

re.IsMatch("This is someText, followed by someAnotherText with text after.");
re.IsMatch("someTextsomeAnotherText");
re.IsMatch("start:someTextsomAnotherText:end");

And so on.

In Regex terms * is a quantifier. Other quantifiers are:

?     Match 0 or 1
+     Match 1 or more
{n}   Match 'n' times
{n,}  Match at least 'n' times
{n,m} Match 'n' to 'm' times

All apply to the preceding term in the Regex.

Placing a ? after another quantifier (including ?) will convert it to lazy form, where it will match as few items as it can. This will allow following terms to also match the terms you specified.

Upvotes: 12

Related Questions