Home > other >  Match a number which comes before a string or after a string Regex
Match a number which comes before a string or after a string Regex

Time:07-11

I have a string

random -45 some 5-5-4-4-3-3-2-2-1-1 66 random 45- words 

I want to match all numbers which comes before - or after it.

In my case, I need all the numbers except the number 66 as that number does not have - before or after it.

My regex

\d (-)

This regex picks all the numbers where - comes after a number

(-)\d

This regex picks all the numbers where - comes before a number

How can I combine these two in one regex?

CodePudding user response:

The following regular expression will work

import re

s = "random -45 some 5-5-4-4-3-3-2-2-1-1 66 random 45- words"

numbers = re.findall(r"\d (?=-)|(?<=-)\d ", s)
# ['45', '5', '5', '4', '4', '3', '3', '2', '2', '1', '1', '45']

It has two alternatives delimited by the |. The first alternative matches digits that are followed by a -. This is done with a positive lookahead. The second alternative matches digits preceded by a - using a positive lookbehind.


A naïve approach may have been \d -|-\d , but this fails to match the 1 right before the 66. This is because the previous match already "consumed" the - character. Using lookaheads/lookbehinds avoids this problem.

  • Related