[英]I am trying to extract a substring using regex_replace command . I want to extract “SWIFTInward” here
我在 Ansible 中尝试了以下代码
---
- hosts: windows
strategy: linear
vars:
war_file_path: F:\\Install\\IIBProjects\\DE\\WAR\\DE_SWIFTInward-202010.0.0.war
tasks:
- name: Get properties folder path for windows
set_fact:
endpointDetails: "{{ item | win_basename | regex_replace('\\\\(?:(?!\\\\).)*$', '') }}"
with_items:
- "{{ war_file_path }}"
register: war_file_name
when: ansible_os_family == "Windows"
期望输出: SWIFTInward
我得到的错误输出: DE_SWIFTInward-202010.0.0.war
win_basename
返回没有目录详细信息的文件名。 因此,您的正则表达式与它不匹配,因为它在开始时需要一个\\
。
你需要
endpointDetails: "{{ item | win_basename | regex_replace('^[^_]*_([^_-]+).*', '\\1') }}"
查看正则表达式演示
或者,将regex_search
与简单的_([^_-]+)-
regex 一起使用:
endpointDetails: "{{ item | win_basename | regex_search('(?<=_)[^_-]+') }}"
细节
^[^_]*_([^_-]+).*
:
^
- 字符串的开始[^_]*
- 除_
以外的零个或多个字符_
- 一个_
字符([^_-]+)
- 第 1 组 ( \\1
):除_
和-
之外的一个或多个字符.*
- 字符串的其余部分。(?<=_)[^_-]+
:
(?<=_)
- 字符串中紧跟在_
前面的位置[^_-]+
- 除_
和-
之外的一个或多个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.