Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract all numbers (int and floats) after specific word

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]

like image 305
yair_elmaliah Avatar asked Jan 24 '23 05:01

yair_elmaliah


1 Answers

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.

like image 160
Jan Avatar answered Feb 05 '23 18:02

Jan