使用 regex_findall 将文本拆分为单词
Split a text to words with regex_findall
我想做的是将文本拆分成单词。
我不想只用 space 拆分它,而是用其他分隔符(例如“=”)拆分它,并且我不想在有点 (.) 时拆分它,因为我想将 IP 保留为一个完整的单词。
包含我要拆分的单词的文件示例是
> cat myconfig.xtx
key1=192.168.0.1
key2 = 192.168.0.2
key3="192.168.0.3"
key4 = "192.168.0.4"
从终端,我运行有点像
grep -oE '(\w|\.)+' mycongig.txt
和预期的一样 运行,即使我不完全理解为什么 :)
但是当我试图在带有 regex_findall 的 jinja2 模板中使用它时,我无法使其工作。
我已经在 ansible 中尝试过类似的东西(以及其他几次尝试)
- name: Split words
debug:
msg: "{{ lookup('file', item) | regex_findall('(\w|\.)+') }}"
loop: "{{ files }}"
有什么想法吗?
提前谢谢你
地图regex_findall,例如
- debug:
msg: "{{ lookup('file', 'mycongig.txt').splitlines()|
map('regex_findall', '[\w\.]+')|
list }}"
给予
msg:
- - key1
- 192.168.0.1
- - key2
- 192.168.0.2
- - key3
- 192.168.0.3
- - key4
- 192.168.0.4
我想做的是将文本拆分成单词。 我不想只用 space 拆分它,而是用其他分隔符(例如“=”)拆分它,并且我不想在有点 (.) 时拆分它,因为我想将 IP 保留为一个完整的单词。 包含我要拆分的单词的文件示例是
> cat myconfig.xtx
key1=192.168.0.1
key2 = 192.168.0.2
key3="192.168.0.3"
key4 = "192.168.0.4"
从终端,我运行有点像
grep -oE '(\w|\.)+' mycongig.txt
和预期的一样 运行,即使我不完全理解为什么 :) 但是当我试图在带有 regex_findall 的 jinja2 模板中使用它时,我无法使其工作。
我已经在 ansible 中尝试过类似的东西(以及其他几次尝试)
- name: Split words
debug:
msg: "{{ lookup('file', item) | regex_findall('(\w|\.)+') }}"
loop: "{{ files }}"
有什么想法吗? 提前谢谢你
地图regex_findall,例如
- debug:
msg: "{{ lookup('file', 'mycongig.txt').splitlines()|
map('regex_findall', '[\w\.]+')|
list }}"
给予
msg:
- - key1
- 192.168.0.1
- - key2
- 192.168.0.2
- - key3
- 192.168.0.3
- - key4
- 192.168.0.4