문제

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!

도움이 되었습니까?

해결책

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 >']

다른 팁

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 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top