1

My question is how can i make my regular expression to use a part of a string to find a new match even if this string is already choosed as a match, i don't know how to clarify it more than that, consider this example: I have a string which is

1+2-3*4/5

and i want to match every digit which is surrounded by two operator, my regular expression would be:

(([+]|[-]|[*]|[/])(\d)([+]|[-]|[*]|[/]))

which gives 2 matches, while i see 3 ('+2-','-3*' and '*4/'), this is because my pattern theoretically deletes a part of my string (the previous matche(s)) and use the rest of my string in the next searching. How do i enforce it to find ALL possible matches?

Demo

Any solution for my question/example would be very appreciated.

Mehdi
  • 41
  • 9
  • 1
    If matching only the numbers is sufficient, you could use `(?<=[+*/-])\d(?=[+*/-])`, using lookarounds. If you also want to match the operators, you might want to look at https://stackoverflow.com/questions/5616822/python-regex-find-all-overlapping-matches – Sebastian Proske Aug 31 '17 at 13:40
  • I think you want to use positive lookaheads, read up on those. – sniperd Aug 31 '17 at 13:46
  • Yes, lookarounds/aheads solved my problem. – Mehdi Aug 31 '17 at 13:52

0 Answers0