这是我尝试从中获取信息的文件中两行的示例。
...
{ "SubtitleSettings_REPOSITORY", FieldType_STRING, (int32_t)REPOSITORY},
{ "PREFERRED_SUBTITLE_LANGUAGE", FieldType_STRING,SUBTITLE_LANGUAGE},
...
我想要做的是找出给定字符串的第三个字段与第一个字段匹配,即
SubtitleSettings_REPOSITORY => REPOSITORY
PREFERRED_SUBTITLE_LANGUAGE => SUBTITLE_LANGUAGE
我的Python代码中的regx只能处理第二行,但不能处理第一行。我怎么能改进它?
import re
...
#field is given a value in previous code, can be "SubtitleSettings_REPOSITORY", or "PREFERRED_SUBTITLE_LANGUAGE"
match = re.search(field+'"[, \t]+(\w+)[, \t]+(\w+)', src_file.read(), re.M|re.I)
return_value = match.group(2)
答案 0 :(得分:0)
import re
with open("input.txt") as f:
pattern = "\{ \"(.+)\",.+,(.+)\}"
for line in f:
first, third = re.findall(pattern, line.strip())[0]
print first.strip(), "=>", third.strip()
打印
SubtitleSettings_REPOSITORY => (int32_t)REPOSITORY
PREFERRED_SUBTITLE_LANGUAGE => SUBTITLE_LANGUAGE
input.txt
包含
{ "SubtitleSettings_REPOSITORY", FieldType_STRING, (int32_t)REPOSITORY},
{ "PREFERRED_SUBTITLE_LANGUAGE", FieldType_STRING,SUBTITLE_LANGUAGE}
<强>故障:强>
\{ \"(.+)\"
匹配结构 {+ space +“+ text +”并提取文字 ,.+,(.+)\}
匹配结构,+ text1 +,+ text2 +} 的字符串,并提取 text2 答案 1 :(得分:0)
你可以插入(?:\(\w+\))?
,它允许(并忽略)括号中的可选单词:
match = re.search(field+'"[, \t]+(\w+)[, \t]+(?:\(\w+\))?(\w+)', line, re.M|re.I)
这样,该行匹配,您可以根据需要获得'REPOSITORY'
。