-3

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

Nik
  • 3
  • 2

1 Answers1

-1

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
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • Thanks a lot for the quick reply, but the code you suggested is only able to get the one result from w3w3ww55 part of the code i.e w3w where as I am trying to pull in 3 results i.e w3w 3w3 w3w. – Nik Feb 26 '19 at 17:49
  • I tried using the global (g) code as I was hoping it would not stop after the match has been found but would rather go on. But it did not help. – Nik Feb 26 '19 at 17:54
  • @ Pushpesh Kumar Rajwanshi Apologies if I am not clear, the code you suggested is pulling 3 results, but it seems to stop after it reaches w3w, but I would like it to continue after that to produce 2 more results i.e 3w3 and w3w apart from the 3 results it is producing currently. – Nik Feb 26 '19 at 18:02
  • @ Pushpesh Kumar Rajwanshi Oh, thanks a lot for the information. That is very useful. – Nik Feb 26 '19 at 18:04
  • @ Pushpesh Kumar Rajwanshi So we would be trying to break the string into multiple groups and then try and search through them? – Nik Feb 26 '19 at 18:07
  • @Nik: Try this regex, `(.)(?=(.\1))` but here you will need to append group1 and group2 text and that way you will get all the matches you are looking for. [Check this Demo](https://regex101.com/r/ZUPYcI/2) – Pushpesh Kumar Rajwanshi Feb 26 '19 at 18:07
  • @Downvoter: Don't mind your downvote but leave a message with your lesson? – Pushpesh Kumar Rajwanshi Feb 26 '19 at 18:10