python - simple regex to find two words -
Sentence = "Dr. Peter is a great person, Dr. Mud Lumpert Mercury is a great man." For the meter again ("(Dr. | med.) \ S ([AZ] [az] +)", the sentence 'print'% 02d-% 02d:% s'% (m.start) , M.and (), M.G.P. (2)) This code gives me all the words after "Dr." or "Made" if word starts with capital Now after the string I need output for two words. If both of them started with a capital again, I tried some things: Reader in the meter ("(Dr. | Med.) \ S (([AZ] [Az] +) | ([AZ] [AG] +) \ s ([AZ] [az] +)), print: '% 02d-% 02d:% s'% (m.start) , m.end (), m.group (2, 3)) You can see how I got there. How can I reach "Lupeate Mercury" but also "Peter"?
Now I need one word or two words after "Dr.". And "Made".
I need to get "Peter" and "Lumpart Mercury".
Use non-capturing groups, make it optional within the original capturing group.
& gt; & Gt; & Gt; Import re & gt; & Gt; & Gt; S = "Dr. Peter is a great person, Dr. M. Lumpert Mercury is a great man." & Gt; & Gt; & Gt; For the meter in re.finditer ("(?:: | Med) \ s * ([AZ] [az] + (?: [Az] [az] +)?)", S): ... print ' % 02d-% 02d:% s'% (m.start (), m.end (), m.group (1)) 00-09: Peter 30-50: Lupeate Mercury < / Div>
Comments
Post a Comment