[英]Python 2.7 Regular Expressions: Extract 3rd Hex Value from Text String
[英]Regular expressions to select text from string in Python
我已经格式化了文本行,即
[[item1 *,* {_item2*} *;{item3*}* ;{item4*}*]]
其中*表示单词和方括号之间的任何文本。 是否可以将文本从*收集到变量中?
item1, after1, before2, item2, after2, item3, after3, item4, after4, afterall = re. ???
您应该能够使用正则表达式执行此操作。
http://docs.python.org/library/re.html
您可以在要稍后提取的表达式的部分周围加上括号。
您是要抓住*零件还是零件零件? 如果您尝试抓住*部分,应该不会太难。
import re
reg = r'\[\[item1 (.*),(.*) {_item2(.*)} (.*);{item3(.*)}(.*) ;{item4(.*)}(.*)\]\]'
match = re.match(reg, text)
# You grab items by index. Starting from 1, 0 is the entire match
item1 = match.group(1)
item2 = match.group(2)
您可能需要花一点时间才能使其与您想要的匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.