[英]Python RegEx to find a string between < and >
我有一個Python
應用程序,試圖在其中解析<
和>
之間的信息。
我的字符串是:
##MESSAGE=<A=test_id,B=.,C=type,D="Description">\n
我已經嘗試過Python
:
pattern = re.compile('*\<(\w+)\>*')
match = pattern.match(line)
但是我的模式仍然看起來不正確,因為match
總是等於None
。
誰能看到我的模式中存在的問題?
pattern = re.compile('<(\w+)>')
要么
pattern = re.compile('<([^>]*)>')
您可以通過以下方式使用模式:
使用re.search()
x = re.search(pattern, input)
print x.group(1)
使用re.match()
:您必須在正則表達式的兩端添加.*
。 參見示例:
pattern = re.compile('.*<([^>]*)>.*')
# ^^ ^^
x = pattern.match(input)
print x.group(1)
請改用re.search
, re.match
嘗試從行首開始進行匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.