Search code examples
pythonregexdata-extraction

Extract numbers and the neighboring words from text data in python


I have text string data in a column of data frame

Text Column
The doctor has advised the patient to pay $200 for the treatment
Patient submitted the $200 operation fee but not the admit fee  $100

From above, how to extract numbers and the word before and after that number and store them in separate variables

Expected Results
Var1                 Var2     
pay $200 for
the $200 operation   fee $100

Solution

  • ([a-zA-Z]+) *?\$?([0-9]+) ?([a-zA-Z]*)
    

    Try it out online