Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract substring with regular expression in Python [duplicate]

Tags:

python

regex

How to extract a substring after keyword am, is or are from a string but not include am, is or are?

string = 'I am John'

I used:

re.findall('(?<=(am|is|are)).*', string)

An error occurs

re.error: look-behind requires fixed-width pattern

What is the correct approach?

like image 411
Chan Avatar asked Jan 26 '23 05:01

Chan


1 Answers

import re

s = 'I am John'

g = re.findall(r'(?:am|is|are)\s+(.*)', s)
print(g)

Prints:

['John']
like image 104
Andrej Kesely Avatar answered Jan 30 '23 04:01

Andrej Kesely