1

I am new to regular expressions and would like to know the pattern to find if a wildcard string exists in another string? Eg:
if * search_string in main_string (if the main string ends with search_string)
if search_string * in main_string (if main string starts with search_string)
if * search_string * in main_string (if main string contains search_string)
if search_string in main_string (if main string itself is search_string)

search_string can be any string, even something like this "(13786213" or "34576(13786213", should work in any case of strings?

I tried this, but not a generic one, it fails for the "(13786213" case etc.

string search_string = search_string.Replace("*", ".*?");
if(new Regex(search_string.ToLower()).IsMatch(main_string))
{
   \\success
}
jithu
  • 151
  • 11

1 Answers1

3

You may split with *, then escape each chunk, and then join with .*?.

var search_string = string.Join(".*", search_string.Split('*').Select(v => Regex.Escape(v)));

Then, to make case insensitive search, compile the Regex object with RegexOptions.IgnoreCase flag.

Here is a C# demo:

var search_string = "(137*86213";
search_string = string.Join(".*", search_string.Split('*').Select(v => Regex.Escape(v)));
if(Regex.IsMatch("(137 text here 86213", search_string, RegexOptions.IgnoreCase))
{
   Console.Write("success");
}
// => success

If the input strings can contain newlines, also add the RegexOptions.Singleline option.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thanks Wiktor for this solution, let me test with various cases and shall adapt further :) – jithu Jul 28 '17 at 12:23
  • Question may be a stupid one, but is there any way to skip, if I actually have a * within the search_string and a wildcard (*) :) :) – jithu Jul 28 '17 at 12:27
  • @jithu Do you mean your wildcard is defined as `(*)`? Then you need to split with `.Split(new[] {"(*)"}, StringSplitOptions.None)`. See [this answer](https://stackoverflow.com/questions/38974292/manual-string-split-in-c-sharp/38974310#38974310). – Wiktor Stribiżew Jul 28 '17 at 12:29
  • Not really, I have a * in search_string and the wildcard is also the same...does it work somehow? – jithu Jul 28 '17 at 12:32
  • Sorry, it is a design issue, you must think of an unambiguous way of defining wildcards. And I have shown a way: make the `(*)` a wild card. Or escape it. – Wiktor Stribiżew Jul 28 '17 at 12:34
  • Okay, it's clear then!! I need to define a unique wildcard character, let me check again, and also will this really work in case of search_string = "34576(13786213" – jithu Jul 28 '17 at 12:35
  • Yes, it will because if `Split` does not find a delimiter in the input string, there will be only 1 item in the resulting array. – Wiktor Stribiżew Jul 28 '17 at 12:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150406/discussion-between-jithu-and-wiktor-stribizew). – jithu Jul 28 '17 at 12:45
  • There has been a question about it - [here it is](https://stackoverflow.com/questions/45000853/matchevaluator-gives-cannot-use-a-lambda-expression-error/45001136#45001136). Also, see [this answer](https://stackoverflow.com/a/32056864/3832970). – Wiktor Stribiżew Jul 28 '17 at 12:49
  • Yeah, that was the issue, I just had to cast it actually, Thanks :) – jithu Jul 28 '17 at 13:25
  • Wiktor, this solution fails in case of search_string*, it does not see whether the main_string starts with, but it returns true if I have the search_str anywhere in the main_string. So giving the wildcard does not really make any change. It just look for the search_string. Actually if you see my question description, there I had mentioned this requirement. Do you know if this is possible somehow? – jithu Aug 03 '17 at 10:29
  • Yes, the `.*?` at the end of the pattern should be changed into `.*`. Actually, you may just use `.*` instead of `.*?`. `string.Join(".*", search_string.Split('*').Select(v => Regex.Escape(v)))` – Wiktor Stribiżew Aug 03 '17 at 10:46
  • 1
    I see, thanks for the quick response, let me test this again! – jithu Aug 03 '17 at 11:09