0

I tried several solutions from the internet an worked myself through some tutorials but I am not able to make it work. I try to match a word in a string with random letters, numbers or dots before and/or after.

eg.

Meeting.Room

MeetingRoom21

Room

Meeting2Room

Meeting.room

12MeetingRoom110.MeetingRoom

I try to match the word "Room" but it should not be case sensitive.

The last pattern I tried was this: \b()(\wRoom\w)\b \ig

But I use regex not that much and I struggle to solve something after three months.

I hope someone can help me.

 public bool Regex_check_for_match(string input, string pattern)
 {
     bool ismatch = Regex.IsMatch(input, pattern);
     return ismatch;
 }
Shazy
  • 147
  • 1
  • 10
  • 1
    Regex won't help much here, since you basically just want to check for `Room` anywhere. A general regex pattern would be `\S*room\S*`, but you don't even need regex. – Tim Biegeleisen Jul 11 '19 at 14:05
  • Can the word "room" be surrounded by literally anything and still be considered a match? If so, then the current answers/comments are correct. Regex isn't necessary. If you're limited by what content can surround "room", then regex becomes a valuable tool. – Broots Waymb Jul 11 '19 at 14:07

4 Answers4

4

So you want to see if the input contains the word room? In which case there is no need for regex, just do a simple index check

return culture.CompareInfo.IndexOf(input, "room", CompareOptions.IgnoreCase) >= 0

Where culture is the instance of CultureInfo.

andyb952
  • 1,931
  • 11
  • 25
3

Why use a regex? Just use .Contains...

public bool CheckForMatch(string input, string pattern)
{
    return input.Contains(pattern, StringComparison.OrdinalIgnoreCase);
}
Milney
  • 6,253
  • 2
  • 19
  • 33
  • 1
    I didn't think contains had an out of the box option for ignorecase? – andyb952 Jul 11 '19 at 14:06
  • 1
    Oops, your right, I have that as an extension method and got so used to using it - It uses the code from your answer 'under-the-hood' – Milney Jul 11 '19 at 14:14
1

My guess is that maybe you might want to design an expression to find room in different settings, such as:

\B(room)\B|\b(room)\b|(room)

Example

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"\B(room)\B|\b(room)\b|(room)";
        string input = @"Meeting.Room
MeetingRoom21
Room
Meeting2Room
Meeting.room
12MeetingRoom110.MeetingRoom";
        RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

The expression is explained on the top right panel of this demo, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs step by step, if you like.

Emma
  • 27,428
  • 11
  • 44
  • 69
0

FYI To match numerics, use '\d' and for non-alpha characters use '\W', to define a matching group use parentheses () and square brackets for optional matches

Using http://regexstorm.net/tester I created a regex ... but I'm confused why you would need to that when you could merely use indexOf instead

To ignore the prefacing elements you can use

[\d\W]*(Meeting)?(Room)
schlock
  • 519
  • 3
  • 5
  • 14