Assuming I have the following string:
str = """
HELLO 1 Stop #$**& 5.02‼️ 16.1
regex
5 ,#2.3222
"""
I want to export all numbers , Whether int or float after the word "stop" with no case sensitive . so the expected results will be :
[5.02, 16.1, 5, 2.3222]
The farthest I have come so far is by using PyPi regex from other post here:
regex.compile(r'(?<=stop.*)\d+(?:\.\d+)?', regex.I)
but this expression gives me only [5.02, 16.1]
Yet another one, albeit with the newer regex
module:
(?:\G(?!\A)|Stop)\D+\K\d+(?:\.\d+)?
See a demo on regex101.com.
In Python
, this could be
import regex as re
string = """
HELLO 1 Stop #$**& 5.02‼️ 16.1
regex
5 ,#2.3222
"""
pattern = re.compile(r'(?:\G(?!\A)|Stop)\D+\K\d+(?:\.\d+)?')
numbers = pattern.findall(string)
print(numbers)
And would yield
['5.02', '16.1', '5', '2.3222']
Don't name your variables after inbuilt-functions, like str
, list
, dict
and the like.
If you need to go further and limit your search within some bounds (e.g. all numbers between Stop
and end
), you could as well use
(?:\G(?!\A)|Stop)(?:(?!end)\D)+\K\d+(?:\.\d+)?
# ^^^ ^^^
See another demo on regex101.com.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With