You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python正则提取含image键的Jinja2 if语句块?

Extract Jinja2 if Blocks Containing image: Key from Kubernetes Deployment YAML

Your goal is to pull out only the Jinja2 if blocks from a Kubernetes Deployment YAML that include an image: key, but your initial regex didn't cut it—it was too broad and didn't properly handle block boundaries or the requirement to check for the image: line inside each block.

Let's fix that with a targeted regex approach plus a simple filter step:

import re

with open(args.dataFileName) as fd:
    data = fd.read()

# Regex to capture full Jinja2 if blocks (handles whitespace variations in template tags)
if_block_pattern = re.compile(
    r'{{-\s?if\s+.+?-\s?}}\n.*?{{-\s?end\s?-\s?}}',
    re.DOTALL | re.MULTILINE
)

# Capture all top-level if blocks from the YAML
all_if_blocks = if_block_pattern.findall(data)

# Filter to keep only blocks that contain the 'image:' key
target_blocks = [block for block in all_if_blocks if 'image:' in block]

# Print the final result
for block in target_blocks:
    print(block)

Regex Breakdown

Let's unpack the pattern to understand how it works:

  • {{-\s?if\s+.+?-\s?}}: Matches the opening Jinja2 if tag, accounting for optional whitespace around hyphens and the if keyword. The non-greedy .+? ensures we stop at the closing }} of the tag line.
  • \n.*?: Matches the newline after the opening tag, then any content (non-greedily) to avoid over-matching across multiple blocks.
  • {{-\s?end\s?-\s?}}: Matches the closing end tag, again handling flexible whitespace in template syntax.
  • re.DOTALL lets . match newlines, and re.MULTILINE supports line-based pattern matching.

Output

Running this code against your provided YAML will produce exactly the blocks you're expecting:

{{- if .Values.k8swait.enabled }}
serviceAccountName: {{ template "fullname" . }}-admin
initContainers:
- env:
  - name: CLUSTER
    value: "{{ .Values.k8swait.parameters.cluster}}"
  - name: NAMESPACE
    value: "{{ .Release.Namespace }}"
  - name: RESOURCE
    value: "{{ .Values.k8swait.parameters.resource}}"
  - name: RNAME
    value: "{{ .Values.k8swait.job.jobname }}"
  - name: TIMEOUT
    value: "{{ .Values.k8swait.parameters.timeout}}"
  - name: FREQUENCE
    value: "{{ .Values.k8swait.parameters.frequence}}"
name: {{ .Values.k8swait.parameters.name}}
image: "{{ .Values.global.registry1 }}/{{ .Values.k8swait.repo }}:{{ .Values.k8swait.tag }}"
resources:
  limits:
    cpu: "{{ .Values.resources.limits.cpu }}"
    memory: "{{ .Values.resources.limits.memory }}"
  requests:
    cpu: "{{ .Values.resources.requests.cpu }}"
    memory: "{{ .Values.resources.requests.memory }}"
{{- end }}
{{- if .Values.pvc.enabled }}
image: "{{ .Values.global.registry1 }}/{{ .Values.k8swait.repo }}:{{ .Values.k8swait.tag }}"
volumeMounts:
- mountPath: /BACKUP
  name: "{{ template "fullname" . }}"
{{- end }}

内容的提问来源于stack exchange,提问作者MelDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:49:36