regex - regular expression for title case - Python -
i need find combination of 2 consecutive title case words.
this code far,
text='hi name moh shai , python code regex , needs expertise' rex=r'[a-z][a-z]+\s+[a-z][a-z]+' re.findall(rex,text)
this gives me,
['moh shai', 'this is', 'python code', 'needs some']
however, need combinations. like,
['moh shai', 'this is', 'python code', 'needs some','some expertise']
can please help?
you can use regex lookahead in combination re.finditer
function in order desired outcome:
import re text='hi name moh shai , python code regex , needs expertise' rex=r'(?=([a-z][a-z]+\s+[a-z][a-z]+))' matches = re.finditer(rex,text) results = [match.group(1) match in matches]
now results contain information need:
>>> results ['moh shai', 'this is', 'python code', 'needs some', 'some expertise']
edit: it's worth, don't need finditer
function. can replace bottom 2 lines previous line re.findall(rex,text)
same effect.
Comments
Post a Comment