0

Suppose I have a string like this 123456789. Is there any way to find the every possible existing pattern from the given string like this

1234, 2345, 3456,4567, 5678, 6789 using regex

I did this but couldn't figure out the solution:

import re
regex = r"[1-9]{4}"
test_string = "123456789"
print(re.findall(regex, test_str))

this outputs:

['1234', '5678']

Any help will mean a lot to me, Thanks

cs95
  • 379,657
  • 97
  • 704
  • 746
ShivaGaire
  • 2,283
  • 1
  • 20
  • 31

1 Answers1

4

You can try this regex:

\d(?=(\d{3}))

Regex Demo

Sample Source (Run here )

import re
regex = r"\d(?=(\d{3}))"
test_str = "123456789"
matches = re.finditer(regex, test_str)

for match in matches:
 print(match.group()+match.group(1));
Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43