tags:

views:

65

answers:

2

I want that find empty tags, here is a example

txt ="<lol1><><lol2>"
rgx = "<([a-zA-Z_0-9]+)>"
print re.findall(rgex, txt)

I get this

['lol1', 'lol2']

I want

['lol1', '', 'lol2']

How I can do this with regex?

+5  A: 

Use rgx = "<([a-zA-Z_0-9]*)>"

The key point is using *, which means "zero or more of the preceding", where you're using +, which means "one or more".

Alex Martelli
thanks, I learned something new
Alquimista
@Alquimista, you're welcome!
Alex Martelli
A: 

no need regex

>>> s="txt ="<lol1><><lol2>"
>>> for i in txt.split(">"):
...     if "<" in i:
...        print i[i.find("<")+1:]
...
lol1

lol2
>>> [i[i.find("<")+1:] for i in txt.split(">") if "<" in i ]
['lol1', '', 'lol2']
ghostdog74
`i[i.find("<")+1:]` can be replaced with `i.lstrip('<')` or `i[1:]`. `if "<" in i` can be eliminated or replaced with `if i.startswith("<")`, which is more efficient and to the point.
Mike Graham
I know, but is a more complicated code that the one here, thanks.
Alquimista