I am trying to extract any word before Y
which is boundary separated. As I am trying to consider each line as a separate record using (?m)
flag and trying to capture \w+
which is look ahead by \s+Y
,but I am only able to print 1st match, not the 2nd match(IMP1
).
print(foo)
this is IMP Y text
and this is also IMP1 Y text
this is not so IMP2 N text
Y is not important
Current fruitless attempt:
>>> m = re.search('(?m).*?(\w+)(?=\s+Y)',foo)
>>> m.groups()
('IMP',)
>>>
>>> m = re.search('(?m)(?<=\s)(\w+)(?=\s+Y)',foo)
>>> m.groups()
('IMP',)
>>>
Expected result Is:
('IMP','IMP1')