gpt4 book ai didi

python,提取最外面的括号

转载 作者:太空宇宙 更新时间:2023-11-03 17:55:44 25 4
gpt4 key购买 nike

我有以下代码尝试从模板文件中提取 msgids:

>>> import re
>>> string1 = '''p=t("Because of the high quality surface finish, our garments perform particularly well with digital direct-to-garment (DTG) printing.")'''
>>> string2 = '''p=t("The carbon footprint (CO2e) is the total carbon dioxide, methane, nitrous oxide, and other greenhouse gases emitted during the cultivation and harvesting of cotton, fibre processing, textile production, packaging, transportation and warehousing. In January 2008, Clothing Co. became the first company in the world to calculate the carbon footprint and place the Carbon Reduction Label on textile products.")'''
>>> string3 = '''USER_REGISTERED_AND_ACTIVE: "The email address entered is already registered. <br>Please <a href='/login'>(Login)</a>"'''
>>> string4 = '''p!=t("We received your application to become an additional user for an account on <strong>__date__</strong>.")'''
>>> string5 = '''p=#{t("We received your order on")}'''
>>> gettext_messages = re.compile(r"""\:(.*)""", re.MULTILINE).findall
>>> for string in [string1, string2, string3, string4, string5]:
... msgids = gettext_messages(string)
... print msgids
...
[]
[]
[' "The email address entered is already registered. <br>Please <a href=\'/login\'>(Login)</a>"']
[]
[]
>>> gettext_re = re.compile(r"""[=|#]t\((.*?)\)""").findall
>>> for string in [string1, string2, string3, string4, string5]:
... msgids = gettext_re(string)
... print msgids
...
['"Because of the high quality surface finish, our garments perform particularly well with digital direct-to-garment (DTG']
['"The carbon footprint (CO2e']
[]
['"We received your application to become an additional user for an account on <strong>__date__</strong>."']
[]

这在一定程度上是有效的,因为如果一个字符串包含另一组 ( ) 那么它就会失败。

非常感谢任何建议

最佳答案

好吧,应该是这样

gettext_re = re.compile(r"""[=|#|{]t\((.*?)\"\)""").findall

关于python,提取最外面的括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28473972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com