是否可以在python中绘制正则表达式来提取所有字符串的属性值

问题描述 投票:0回答:2

我有一个像这样的xml文件:

<string name="abc_searchview_description_clear">Clear query</string>
<string name="abc_searchview_description_query">Search query</string>
<string name="abc_searchview_description_search">Search</string>
<string name="abc_searchview_description_submit">Submit query</string>
<string name="abc_searchview_description_voice">Voice search</string>
<string name="abc_shareactionprovider_share_with">Share with</string>

我想在python3中找到所有字符串值,即[清除查询,搜索查询,搜索,提交查询,语音搜索,与...共享]。

我只是想知道是否有可能为它绘制正则表达式? {类似于我们在double qoute中找到所有文本的内容[Find_double_quotes = re.compile('“([^”] *)“',re.DOTALL | re.MULTILINE | re.IGNORECASE)}

python xml python-3.x string
2个回答
1
投票

如果你的用例就像这里显示的那样简单(即没有嵌套,没有其他标签),这应该这样做re.findall("<string[^>]*>(.*)</string>", your_text)

虽然,XML不是常规语言,但正则表达式会因一些更普遍的情况而失败。如果您需要更复杂的情况,可以考虑不使用正则表达式。有关更详细的解释,请参阅this question


0
投票

非常感谢,它对我来说很好。

我也试试这个:Find_double_quotes = re.compile('“>([^>] *)

它也产生相同的结果

© www.soinside.com 2019 - 2024. All rights reserved.