chr*_*ris 3 python string text-processing
我有一个文本文件,其中包含{[]}标记内的数据.解析该数据的建议方法是什么,以便我可以只使用标签内的数据?
示例文本文件如下所示:
'这是一堆在任何{[way]}中都没有{[really]}有用的文本.我需要{[get]}一些项目{[from]}."
我想在列表中以"真实","方式","获取","来自"结束.我想我可以用split来做它..但似乎可能有更好的方法.我看过很多解析库,有没有一个对我想做的事情很完美?
我会使用正则表达式.此答案假定标记字符{} []中没有一个出现在其他标记字符中.
import re
text = 'this is a bunch of text that is not {[really]} useful in any {[way]}. I need to {[get]} some items {[from]} it.'
for s in re.findall(r'\{\[(.*?)\]\}', text):
print s
Run Code Online (Sandbox Code Playgroud)
在python正则表达式中使用详细模式:
re.findall('''
\{ # opening curly brace
\[ # followed by an opening square bracket
( # capture the next pattern
.*? # followed by shortest possible sequence of anything
) # end of capture
\] # followed by closing square bracket
\} # followed by a closing curly brace
''', text, re.VERBOSE)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
786 次 |
| 最近记录: |