我正在尝试使用 regex_replace 命令提取子字符串.我想在这里提取“SWIFTInward" [英] I am trying to extract a substring using regex_replace command . I want to extract “SWIFTInward” here
本文介绍了我正在尝试使用 regex_replace 命令提取子字符串.我想在这里提取“SWIFTInward"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Ansible 中尝试了以下代码
I tried below code in 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
和一个简单的 _([^_-]+)-
正则表达式:
Or, use regex_search
with a simple _([^_-]+)-
regex:
endpointDetails: "{{ item | win_basename | regex_search('(?<=_)[^_-]+') }}"
详情
^[^_]*_([^_-]+).*
:^
- 字符串的开始[^_]*
- 除_
之外的零个或多个字符_
-_
字符([^_-]+)
- 第 1 组 (\1
):除_
和之外的一个或多个字符>-
.*
- 字符串的其余部分.
^[^_]*_([^_-]+).*
:^
- start of a string[^_]*
- zero or more chars other than_
_
- a_
char([^_-]+)
- Group 1 (\1
): one or more chars other than_
and-
.*
- the rest of the string.
(?<=_)
- 字符串中紧跟在_
前面的位置[^_-]+
- 除了_
和-
之外的一个或多个字符.
(?<=_)
- a location in string that is immediately preceded with_
[^_-]+
- one or more chars other than_
and-
.
这篇关于我正在尝试使用 regex_replace 命令提取子字符串.我想在这里提取“SWIFTInward"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文