pythonregexregex-lookaroundspositive-lookahead

extracting word before character


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')

Solution

  • You can use

    \w+(?=[^\S\r\n]+Y\b)
    

    See the regex demo. Details:

    See a Python demo:

    import re
    foo = "this is IMP Y text\nand this is also IMP1 Y text\nthis is not so IMP2 N text\nY is not important"
    print(re.findall(r'\w+(?=[^\S\r\n]+Y\b)', foo))
    # => ['IMP', 'IMP1']