简体   繁体   中英

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.

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.

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

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