Based on some string of numbers:
(30123:424302) 123 #4324:#34123
How can I obtain only the numbers that are NOT immediately preceded by "#"? I have found how to get those numbers preceded by "#" (\#+\d+
) but I need the opposite. Can I group all \d+
and then inverse match based on the pattern I have somehow?
To clarify, I need 30123
, 424302
, and 123
in the above example.
You need
(?<![#\d])\d+
See the regex demo.
Pattern details
(?<![#\d])
- a negative lookbehind that fails the match if there is a digit or a #
char immediately before the current position\d+
- one or more digits.See the Python demo:
import re
text = "(30123:424302) 123 #4324:#34123"
print(re.findall(r"(?<![#\d])\d+", text))
# => ['30123', '424302', '123']
And if you need to "reverse" something the way you originally thought of, you can match what you do not want, and then match and capture what you want, and after collecting the matches, remove all empty values from the resulting list:
import re
text = "(30123:424302) 123 #4324:#34123"
print(list(filter(None, re.findall(r"#\d+|(\d+)", text))))
See this Python demo.
As you can see, #\d+
consumed all digits after the #
(i.e. in the undesired context) and (\d+)
fetched the right values.