[英]I am trying to extract a substring using regex_replace command . I want to extract “SWIFTInward” here
I tried below code in Ansible我在 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"
Expect output : SWIFTInward
期望输出:
SWIFTInward
Error output which i am getting : DE_SWIFTInward-202010.0.0.war
我得到的错误输出:
DE_SWIFTInward-202010.0.0.war
The win_basename
returns the file name without the directory details. win_basename
返回没有目录详细信息的文件名。 So, your regex does not match it as it requires a \\
at the start.因此,您的正则表达式与它不匹配,因为它在开始时需要一个
\\
。
You need你需要
endpointDetails: "{{ item | win_basename | regex_replace('^[^_]*_([^_-]+).*', '\\1') }}"
See the regex demo查看正则表达式演示
Or, use regex_search
with a simple _([^_-]+)-
regex :或者,将
regex_search
与简单的_([^_-]+)-
regex 一起使用:
endpointDetails: "{{ item | win_basename | regex_search('(?<=_)[^_-]+') }}"
Details细节
^[^_]*_([^_-]+).*
: ^[^_]*_([^_-]+).*
:
^
- start of a string ^
- 字符串的开始[^_]*
- zero or more chars other than _
[^_]*
- 除_
以外的零个或多个字符_
- a _
char _
- 一个_
字符([^_-]+)
- Group 1 ( \\1
): one or more chars other than _
and -
([^_-]+)
- 第 1 组 ( \\1
):除_
和-
之外的一个或多个字符.*
- the rest of the string. .*
- 字符串的其余部分。(?<=_)[^_-]+
: (?<=_)[^_-]+
:
(?<=_)
- a location in string that is immediately preceded with _
(?<=_)
- 字符串中紧跟在_
前面的位置[^_-]+
- one or more chars other than _
and -
. [^_-]+
- 除_
和-
之外的一个或多个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.