Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在ansible中从文件中读取文本块_Regex_Ansible_Regex Greedy - Fatal编程技术网

Regex 如何在ansible中从文件中读取文本块

Regex 如何在ansible中从文件中读取文本块,regex,ansible,regex-greedy,Regex,Ansible,Regex Greedy,嗨,我正在从文件中读取内容 该文件包含以下内容 ====== 接口和 公共无效和无效,int 公共无效nandint,int ====== 接口或 public void or(int, int); public void nor(int, int); ====== 接口异或 public void xor(int, int); public void xnor(int, int); ====== 接口不 public void not(int); ====== 类按位扩展A

嗨,我正在从文件中读取内容

该文件包含以下内容

======

接口和

公共无效和无效,int

公共无效nandint,int

======

接口或

 public void or(int, int);

 public void nor(int, int);
======

接口异或

 public void xor(int, int);

 public void xnor(int, int);
======

接口不

 public void not(int);
======

类按位扩展And、Or、Xor、Not

//接口的实现在这里

======

我试图只读取接口

我经历了这一切

程序输出为

ok: [127.0.0.1] => {
    "my_interfaces": [
        "interface And",
        "interface Or",
        "interface Xor",
        "interface Not"
    ]
}
但是期望的输出是

ok: [127.0.0.1] => {
    "my_interfaces": [
        "interface And \n public void and(int, int) \n public void nand(int, int)",
        "interface Or \n public void or(int, int) \n public void nor(int, int)",
        "interface Xor \n public void xor(int, int) \n public void xnor(int, int)",
        "interface Not \n public void not(int)",
    ]
}
我认为我在正则表达式部分做错了什么。但我不知道如何纠正它以获得所需的输出。有人能帮我解决这个问题吗。除此之外,还有其他方法可以完成相同的任务。

在您的模式界面中。+?=*此部分。+?是非贪婪的,因此引擎将尽可能匹配1+次。此部分=*匹配等号0+次

当接口中没有等号时,如果点与换行符不匹配,它将只匹配后跟空格的接口

如果要使用图案,必须启用点与换行符匹配。如果支持,请使用内联修饰符。使用捕获组或正向前瞻,以不匹配换行符和等号,但确保它在那里

(?s)^interface\b.+?(?=\r?\n=)
另一个选项是匹配接口和行的其余部分。然后重复匹配行,只要下一行不是以等号开头,使用负前瞻=


一旦在my_界面中有了标题列表,就可以使用sed并打印行的范围

下面的任务

- command: "sed -n '/{{ item }}/,/======/p' {{ file_path }}"
  register: result
  loop: "{{ my_interfaces }}"
- set_fact:
    my_ifc: "{{ my_ifc|default([]) + [ item.stdout_lines ] }}"
  loop: "{{ result.results }}"
- debug:
    var: my_ifc
给予

格式化在制品…

您可以组合并获得所需的结果:

---
- hosts: localhost
  gather_facts: false
  vars:
      content: "{{ lookup('file', 'filename') }}"
  tasks:
    - name: "split file into blocks"
      set_fact:
          content: "{{ content.split('======') }}"
    - debug:
          msg: "{{ content }}"
    - name: "remove white space from start and end of blocks"
      set_fact:
          content: "{{ content | map('trim') | list}}"
    - debug:
          msg: "{{ content }}"
    - name: "select blocks that start with interface"
      set_fact:
          content: "{{ content | select('search', '^interface') | list}}"
    - debug:
          msg: "{{ content }}"
您还可以在单个命令中组合所有步骤:

---
- hosts: localhost
  gather_facts: false
  vars:
      content: "{{ lookup('file', 'filename') }}"
  tasks:
    - name: "fetch interfaces"
      set_fact:
          content: "{{ content.split('======') | map('trim') | select('search', '^interface') | list }}"
    - debug:
          msg: "{{ content }}"
这将返回:

[u'interface And\n\npublic void and(int, int);\n\npublic void nand(int, int);',
 u'interface Or\n\n public void or(int, int);\n\n public void nor(int, int);',
 u'interface Xor\n\n public void xor(int, int);\n\n public void xnor(int, int);',
 u'interface Not\n\n public void not(int);']

您必须使用点划线换行,并使用1+乘以等号,否则接口后面的所有内容都将是可选的?s^interface\b.+?\r?\n=+。如果支持,请使用?s。看见
---
- hosts: localhost
  gather_facts: false
  vars:
      content: "{{ lookup('file', 'filename') }}"
  tasks:
    - name: "split file into blocks"
      set_fact:
          content: "{{ content.split('======') }}"
    - debug:
          msg: "{{ content }}"
    - name: "remove white space from start and end of blocks"
      set_fact:
          content: "{{ content | map('trim') | list}}"
    - debug:
          msg: "{{ content }}"
    - name: "select blocks that start with interface"
      set_fact:
          content: "{{ content | select('search', '^interface') | list}}"
    - debug:
          msg: "{{ content }}"
---
- hosts: localhost
  gather_facts: false
  vars:
      content: "{{ lookup('file', 'filename') }}"
  tasks:
    - name: "fetch interfaces"
      set_fact:
          content: "{{ content.split('======') | map('trim') | select('search', '^interface') | list }}"
    - debug:
          msg: "{{ content }}"
[u'interface And\n\npublic void and(int, int);\n\npublic void nand(int, int);',
 u'interface Or\n\n public void or(int, int);\n\n public void nor(int, int);',
 u'interface Xor\n\n public void xor(int, int);\n\n public void xnor(int, int);',
 u'interface Not\n\n public void not(int);']