skip to Main Content

I would like to find string matches within another string the following way:

Have a fix string like: "BRB"

Have different – test – sentences like:

  • "What be right b means?" does not match
  • "What b r b means?" match
  • "What BR b means?" match
  • "What BR-b means?" match
  • "what brb means?" match

etc.

So I try to match the fix string within another string where possible whitespace characters (i.e. spaces) occures in between the characters.

How would it be possible to compose the regexp?

I am a newbie regex user so could not figure out how to do.

2

Answers


  1. You could use the following regex pattern, in case insensitive mode:

    b(?i)b[ -]?r[ -]?bb
    

    Demo

    This pattern says to match:

    • b a word boundary
    • (?i) in case insensitive mode
    • b the first letter b
    • [ -] optional space or hyphen separator
    • r the second letter r
    • [ -] optionap space or hyphen separator
    • b third letter b
    • b word boundary
    Login or Signup to reply.
  2. For c# you can use this

    using System;
    using System.Text.RegularExpressions;
    
    public class Example
    {
        public static void Main()
        {
           string pattern = @"b( |-)?r( |-)?b";
           string input = @"what brb means?";
           RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;
        
           foreach (Match m in Regex.Matches(input, pattern, options))
           {
               Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
           }
       }
    }
    

    Regex101

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search