正则表达式提取两个模式之间的线

Regex to extract lines between two patterns

我有剧本输出 (exp.stdout_lines) 作为

    "stdout_lines": [
        "exit",
        "logout",
        "",
        "df START",
        "df -h",
        "Filesystem      Size  Used Avail Use% Mounted on",
        "udev            476M     0  476M   0% /dev",
        "tmpfs           100M   11M   89M  11% /run",
        "/dev/sda1       218G  1.9G  205G   1% /",
        "tmpfs           497M     0  497M   0% /dev/shm",
        "tmpfs           5.0M     0  5.0M   0% /run/lock",
        "tmpfs           497M     0  497M   0% /sys/fs/cgroup",
        "/dev/sda2       923M   59M  802M   7% /boot",
        "/dev/sda4       266G  306M  252G   1% /home",
        "tmpfs           100M     0  100M   0% /run/user/1001",
        "\u001b]0;",
        "",
        "df END",
        "running ls commnd",
        "ls",
        "bhr",
        "\u001b]0;",
        "",
        "completed ls commnd"
    ]
}

我想 grep df STARTdf END

之间的行
 - set_fact: 
        result: "{{ exp.stdout_lines | regex_search((?<=df START).*(?=df END)) }}"

我看到以下错误

"template error while templating string: unexpected char '?' at 36. String: {{ exp.stdout_lines | regex_search((?<=df START).*(?=df END)) }}"}

任何建议。

我想我们可以使用列表切片来代替正则表达式。

所以从 exp.stdout_lines 列表中,我们找到 df STARTexp.stdout_lines.index('df START') 的索引。 df END 也是如此。然后我们可以使用exp.stdout_lines[startIndex + 1 : endIndex]来获取严格在df STARTdf END之间的元素。

- hosts: localhost
  vars:
    exp:
      stdout_lines: [
          "exit",
          "logout",
          "",
          "df START",
          "df -h",
          "Filesystem      Size  Used Avail Use% Mounted on",
          "udev            476M     0  476M   0% /dev",
          "tmpfs           100M   11M   89M  11% /run",
          "/dev/sda1       218G  1.9G  205G   1% /",
          "tmpfs           497M     0  497M   0% /dev/shm",
          "tmpfs           5.0M     0  5.0M   0% /run/lock",
          "tmpfs           497M     0  497M   0% /sys/fs/cgroup",
          "/dev/sda2       923M   59M  802M   7% /boot",
          "/dev/sda4       266G  306M  252G   1% /home",
          "tmpfs           100M     0  100M   0% /run/user/1001",
          "\u001b]0;",
          "",
          "df END",
          "running ls commnd",
          "ls",
          "bhr",
          "\u001b]0;",
          "",
          "completed ls commnd"
      ]
  tasks:
    - debug:
        msg: "{{ exp.stdout_lines[exp.stdout_lines.index('df START') + 1 : exp.stdout_lines.index('df END')] }}"
ok: [localhost] => {
    "msg": [
        "df -h",
        "Filesystem      Size  Used Avail Use% Mounted on",
        "udev            476M     0  476M   0% /dev",
        "tmpfs           100M   11M   89M  11% /run",
        "/dev/sda1       218G  1.9G  205G   1% /",
        "tmpfs           497M     0  497M   0% /dev/shm",
        "tmpfs           5.0M     0  5.0M   0% /run/lock",
        "tmpfs           497M     0  497M   0% /sys/fs/cgroup",
        "/dev/sda2       923M   59M  802M   7% /boot",
        "/dev/sda4       266G  306M  252G   1% /home",
        "tmpfs           100M     0  100M   0% /run/user/1001",
        "\u001b]0;",
        ""
    ]
}

如果您希望结果是字符串而不是列表,您可以将其通过管道传递给 join('\n') 过滤器。