Question

I have several strings like these:

unicode 0, <runtime error >,0
unicode 0, <TLOSS error>
unicode 0, <- Attempt to use MSIL code >
unicode 0, <ModuleNameA>

I am trying to use re to match all the string inside "<" ">"

I tried this :

items = line.split()
pattern = r"<.+?>"
match = re.findall(pattern, items[2])

But it seems that space can not be deal with..

Could anyone give me some help?

Thank you!

Was it helpful?

Solution

items[2] contains partial content:

>>> items = line.split()
>>> items[2]
'<runtime'

Just pass the line to the re.findall:

>>> line = 'unicode 0, <runtime error >,0'
>>> re.findall(r'<.+?>', line)
['<runtime error >']

OTHER TIPS

Just don't split them and use the string as it is, like this

line = "unicode 0, <runtime error >,0"
import re
print(re.findall(r"<.+?>", line))
# ['<runtime error >']

If you want only the string inside that, you can do

print(re.search(r"<(.+?)>", line).group(1))
# runtime error 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top