我正在尝试使用regex_replace命令提取子字符串.我想在这里提取"SWIFTInward" [英] I am trying to extract a substring using regex_replace command . I want to extract “SWIFTInward” here

查看:76
本文介绍了我正在尝试使用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

推荐答案

您需要

endpointDetails: "{{ item | win_basename | regex_replace('^[^_]*_([^_-]+).*', '\\1') }}"

请参见 regex演示

或者,将 regex_search 与一个简单的 _([^ _-] +)-

Or, use regex_search with a simple _([^_-]+)- regex:

endpointDetails: "{{ item | win_basename | regex_search('(?<=_)[^_-]+') }}"

详细信息

  • ^ [^ _] * _([^ _-] +).* :
    • ^ -字符串的开头
    • [^ _] * -除 _
    • 以外的零个或多个字符
    • _ -一个 _ char
    • ([[^ _-] +)-组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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆