regexrubular

Look for any character that surrounds one of any character including itself


I am trying to write a regex code to find all examples of any character that surrounds one of any character including itself in the string below:

b9fgh9f1;2w;111b2b35hw3w3ww55

So ‘b2b’ and ‘111’ would be valid, but ‘3ww5’ would not be.

Could someone please help me out here?

Thanks, Nikhil


Solution

  • You can use this regex which will match three characters where first and third are same using back reference, where as middle can be any,

    (.).\1
    

    Demo

    Edit:

    Above regex will only give you non-overlapping matches but as you want to get all matches that are even overlapping, you can use this positive look ahead based regex which doesn't consume the next two characters instead groups them in group2 so for your desired output, you can append characters from group1 and group2.

    (.)(?=(.\1))
    

    Demo with overlapping matches

    Here is a Java code (I've never programmed in Ruby) demonstrating the code and the same logic you can write in your fav programming language.

    String s = "b9fgh9f1;2w;111b2b35hw3w3ww55";
    Pattern p = Pattern.compile("(.)(?=(.\\1))");
    Matcher m = p.matcher(s);
    
    while(m.find()) {
        System.out.println(m.group(1) + m.group(2));
    }
    

    Prints all your intended matches,

    111
    b2b
    w3w
    3w3
    w3w
    

    Also, here is a Python code that may help if you know Python,

    import re
    
    s = 'b9fgh9f1;2w;111b2b35hw3w3ww55'
    
    matches = re.findall(r'(.)(?=(.\1))',s)
    for m in re.findall(r'(.)(?=(.\1))',s):
     print(m[0]+m[1])
    

    Prints all your expected matches,

    111
    b2b
    w3w
    3w3
    w3w