Home > other >  Python regex match n characters before the keyword
Python regex match n characters before the keyword

Time:06-07

I have a dataframe column looks like this

data
Membership 1 year
Individual - 10 years:
Membership 2019-2024

I want to extract the number before the keyword 'year'. I'd like to get output:

data                      contract_years
Membership 1 year         1
Individual - 10 years:    10
Membership 2019-2024

I tried \d{2} (?=year) which is obviously wrong. Any suggestion will be really helpful, thanks.

CodePudding user response:

df['contract_years'] = df['data'].str.extract('(\d ) year')

CodePudding user response:

Try this one (\d )[ ]*[yY][eE][aA][rR][sS]?

  • Related