[英]regexp extract first group of numbers from line in file
我在這里寫了這段代碼,我想提取粗體數字(第一次出現)進行比較。
- lineinfile:
path: /bin/animals.txt
state: absent
regexp: '(?i)^\s*chicken\s+[0-9]+\s*$'
check_mode: yes
register: egg
- name: comparing number
debug:
msg: "chicken lays {{egg}} egg today."
when: egg > 0
animals.txt 里面(例子)
雞下了53個蛋,吃了 4 個蟲。
更新添加了反向引用
- lineinfile:
path: /bin/animals.txt
state: absent
backrefs: yes
regexp: '(?i)chicken\D*(\d+)'
check_mode: yes
register: egg
- name: comparing number
debug:
msg: "chicken lays {{egg}} egg today."
when: egg > 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.