捕获属性名称

2024-09-28 22:22:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在扫描一个“.twig”(PHP模板)文件并试图捕获对象的属性名

细枝文件包含如下行(字符串):

{{ product.id }}
{{ product.parentProductId }}
{{ product.countdown.startDate | date('Y/m/d H:i:s') }}
{{ product.countdown.endDate | date('Y/m/d H:i:s') }}
{{ product.countdown.expireDate | date('Y/m/d H:i:s') }}
{{ product.primaryImage.originalUrl }}
{{ product.image(1).originalUrl }}
{{ product.image(1).thumbUrl }}
{{ product.priceWithTax(preferences.default_currency) | money }}

我想捕捉的是:

.id
.parentProductId
.countdown
.startDate
.endDate
.expireDate
.primaryImage
.originalUrl
.image(1)
.originalUrl
.thumbUrl
.priceWithTax(preferences.default_currency)

基本上,我正在尝试找出product对象的属性。我有以下模式,但它不捕获链接属性。例如

"{{.+?product(\.[a-zA-Z]+(?:\(.+?\)){,1})++.+?}}"只捕获.startDate,但它应该分别捕获.countdown.startDate。这是不可能的,还是我遗漏了什么

regex101

我可以捕获("{{.+?product((?:\.[a-zA-Z]+(?:\(.+?\)){,1})+).+?}}")它作为一个整体(.countdown.startDate),然后检查/拆分它,但这听起来很麻烦


Tags: 文件对象imageiddate属性productcountdown
3条回答

试试这个,你的需求都能满足

^{{ product(\..*?[(][^\d\/]+[)]).*?}}|^{{ product(\..*?)(\..*?)?(?= )

demo and explanation at regex 101

如果您想用一个regex来处理它,您可能需要使用PyPiregex模块:

import regex

s = """{{ product.id }}
{{ product.parentProductId }}
{{ product.countdown.startDate | date('Y/m/d H:i:s') }}
{{ product.primaryImage.originalUrl }}
{{ product.image(1).originalUrl }}
{{ product.priceWithTax(preferences.default_currency) | money }}"""

rx = r'{{[^{}]*product(\.[a-zA-Z]+(?:\([^()]+\))?)*[^{}]*}}'

l = [m.captures(1) for m in regex.finditer(rx, s)]

print([item for sublist in l for item in sublist])
# => ['.id', '.parentProductId', '.countdown', '.startDate', '.primaryImage', '.originalUrl', '.image(1)', '.originalUrl', '.priceWithTax(preferences.default_currency)']

参见Python demo

{{[^{}]*product(\.[a-zA-Z]+(?:\([^()]+\))?)*[^{}]*}}正则表达式将匹配

  • {{-{{子串
  • [^{}]*-0+除{}以外的字符
  • product-子串product
  • (\.[a-zA-Z]+(?:\([^()]+\))?)*-捕获组1:零个或多个
    • \.-一个点
    • [a-zA-Z]+-1+ASCII字母
    • (?:\([^()]+\))?—可选的(、除()之外的1+字符序列,然后是)
  • [^{}]*-0+除{}以外的字符
  • }}-a}}子串

如果仅限于re,则需要将所有属性捕获到一个捕获组中(将此(\.[a-zA-Z]+(?:\([^()]+\))?)*包装为(...)),然后运行基于regex的post进程,按.而不是括号内进行拆分:

import re
rx = r'{{[^{}]*product((?:\.[a-zA-Z]+(?:\([^()]+\))?)*)[^{}]*}}'
l = re.findall(rx, s)
res = []
for m in l:
     res.extend([".{}".format(n) for n in filter(None, re.split(r'\.(?![^()]*\))', m))])
print(res)
# => ['.id', '.parentProductId', '.countdown', '.startDate', '.primaryImage', '.originalUrl', '.image(1)', '.originalUrl', '.priceWithTax(preferences.default_currency)']

this Python demo

我决定坚持re(而不是维克托建议的regex),这就是我最终得到的结果:

import re, json

file = open("test.twig", "r", encoding="utf-8")
content = file.read()
file.close()

patterns = {
    "template"  : r"{{[^{}]*product((?:\.[a-zA-Z]+(?:\([^()]+\))?)*)[^{}]*}}",
    "prop"      : r"^[^\.]+$",                  # .id
    "subprop"   : r"^[^\.()]+(\.[^\.]+)+$",     # .countdown.startDate
    "itemprop"  : r"^[^\.]+\(\d+\)\.[^\.]+$",   # .image(1).originalUrl
    "method"    : r"^[^\.]+\(.+\)$",            # .priceWithTax(preferences.default_currency)
}

temp_re = re.compile(patterns["template"])
matches = temp_re.findall(content)

product = {}

for match in matches:
    match = match[1:]
    if re.match(patterns["prop"], match):
        product[match] = match
    elif re.match(patterns["subprop"], match):
        match = match.split(".")
        if match[0] not in product:
            product[match[0]] = []
        if match[1] not in product[match[0]]:
            product[match[0]].append(match[1])
    elif re.match(patterns["itemprop"], match):
        match = match.split(".")
        array = re.sub("\(\d+\)", "(i)", match[0])
        if array not in product:
            product[array] = []
        if match[1] not in product[array]:
            product[array].append(match[1])
    elif re.match(patterns["method"], match):
        product[match] = match

props = json.dumps(product, indent=4)

print(props)

输出示例:

{
    "id": "id",
    "parentProductId": "parentProductId",
    "countdown": [
        "startDate",
        "endDate",
        "expireDate"
    ],
    "primaryImage": [
        "originalUrl"
    ],
    "image(i)": [
        "originalUrl",
        "thumbUrl"
    ],
    "priceWithTax(preferences.default_currency)": "priceWithTax(preferences.default_currency)"
}

相关问题 更多 >