2016-03-21 58 views
-1

我有一个类似于此的字符串。包含并忽略2个特殊字符之间的文本

str1=r'<st-button mode="positive" width="small" can-click="{loadConference}" id="meetingPasswordButton">{{i18n "ok"}}</st-button>'

我想包括<>之间的一切,拒绝</>之间的一切。

我试过用这些代码包含和拒绝这些特殊字符之间的文本。但我无法获得输出。帮助我解决这种情况。

代码:

import re 
pat = r'(?<=\<).+?(?=\>)' or r'(?<!\</).+?(?!\>)' 

s = r'<st-button mode="positive" width="small" can-click="{loadConference}" id="meetingPasswordButton">{{i18n "ok"}}</st-button>' 


print (re.findall(pat, s)) 
+2

阅读[此](HTTP: //stackoverflow.com/a/1732454/ 2276527) – Gogo

回答

0

以下正则表达式将<>之间返回数据:

print(re.findall('<((?!/).*?)>', s)) 

输出:

['st-button mode="positive" width="small" can-click="{loadConference}"id="meetingPasswordButton"'] 
+0

非常感谢:) –

0
>>> import re 
>>> s = r'<st-button mode="positive" width="small" can-click="{loadConference}" id="meetingPasswordButton">{{i18n "ok"}}</st-button>' 
>>> print re.findall(r'<([^\/].*?)>', s) 
['st-button mode="positive" width="small" can-click="{loadConference}" id="meetingPasswordButton"']