Using regex expression in python

I am experimenting with regex and I read the statements a bit and saw examples, but for some reason I can't get this to work. I am trying to get the word after the next pattern using look-behind.

import re
s = '123abc456someword 0001abde19999anotherword'
re.findall(r'(?<=\d+[a-z]+\d+)[a-z]+', s, re.I)

The results should be somewordandanotherword

But I get error: look-behind requires fixed-width pattern

Any help was appreciated.

+4
source share
3 answers

Convert it to a group without capture and get the mapped group from index 1.

(?:\d+\w+\d+)(\w+\b)

here demo

[a-z], \w [a-z] . \b .

:

import re
p = re.compile(ur'(?:\d+\w+\d+)(\w+\b)', re.IGNORECASE)
test_str = u"123abc456someword 0001abde19999anotherword"

re.findall(p, test_str)
+3

Python re . , regex :

>>> import regex
>>> s = '123abc456someword 0001abde19999anotherword'
>>> regex.findall(r'(?i)(?<=\d+[a-z]+\d+)[a-z]+', s)
['someword', 'anotherword']

look-behind ( ):

>>> import re
>>> s = '123abc456someword 0001abde19999anotherword'
>>> re.findall(r'\d+[a-z]+\d+([a-z]+)', s, re.I)
['someword', 'anotherword']
+4

lookahead,

>>> import re
>>> s = '123abc456someword 0001abde19999anotherword'
>>> m = re.findall(r'[a-z]+(?= |$)', s, re.I)
>>> m
['someword', 'anotherword']

, .

0

Source: https://habr.com/ru/post/1548274/


All Articles