Regex to get text into python?

2

I would like to extract the text contained between < > of a string. At first, I built the following expression:

import re
m=re.search(r'<(.*)>','abevbv envvrhwkv <eiwbv> ebvi <wieunv> ajhbsvhj')

The expression would be perfect if there were not two texts between < & gt ;. In this case, I will have as a return:

'eiwbv> ebvi <wieunv'

But I want to:

'eiwbv'

What regular expression would I have to use to get this result?

    
asked by anonymous 30.04.2017 / 19:10

2 answers

1

Your RegEx is almost correct, what you did did go wrong is that you used a greedy (greedy) quantifier at the point ( . ).

This causes the regex to search for the last instance where it can be docked, always leaving the captured group as large as possible, you should have used a lazy (lazy) quantifier, it always stops the catch on the first occurrence of the delimiter, in your case ">"

To solve your problem you only have to change the * to *?

import re
m=re.search(r'<(.*)>','abevbv envvrhwkv <eiwbv> ebvi <wieunv> ajhbsvhj')
    
04.05.2017 / 23:37
5

Put a question mark after the asterisk, like this:

m=re.search(r'<(.*?)>','abevbv envvrhwkv <eiwbv> ebvi <wieunv> ajhbsvhj')

print (m)
<_sre.SRE_Match object; span=(17, 24), match='<eiwbv>'>

DEMO

    
30.04.2017 / 20:12