正则表达式匹配反转

huangapple go评论80阅读模式
英文:

Regex match reverse

问题

我有一个字符串

```yaml
  string: Octane_2022.1.0.6_Houdini_19.5.493_OSX.zip

和目前的正则表达式

  regex_search('[0-9]{2}.[0-9]')

这将获取到 2022。期望的结果是 19.5,无论该字段中包含什么内容。

基本上,我需要始终获取此安装程序的基本版本号。当没有年份时,它运行正常,但如何强制我的正则表达式仅在存在 2 位数字 + "." + 1 位数字时获取它?


<details>
<summary>英文:</summary>

I have a string

```yaml
  string: Octane_2022.1.0.6_Houdini_19.5.493_OSX.zip

and regex currently

  regex_search(&#39;[0-9]{2}.[0-9]&#39;)

This will get 2022. The expected result is 19.5 whatever is in that field.

Essentially, I need to always gather the base version number of this installer. It works fine when there is no year in there, but how do I force my regex to only get it if it is 2 digits + "." + 1 digit?

答案1

得分: 2

以下是翻译的部分:

声明变量

```yaml
version_regex: &#39;^\d{2}\.\d{1}.*$&#39;
version: &quot;{{ string|
           split(&#39;_&#39;)|
           select(&#39;regex&#39;, version_regex)|first }}&quot;
major: &quot;{{ version|split(&#39;.&#39;)|first }}&quot;
major_minor: &quot;{{ version|splitext|first }}&quot;
patch: &quot;{{ version|split(&#39;.&#39;)|last }}&quot;

产生结果

version: 19.5.493
major: 19
major_minor: 19.5
patch: 493

详细信息:

给定字符串

string: Octane_2022.1.0.6_Houdini_19.5.493_OSX.zip

拆分字符串

arr: &quot;{{ string|split(&#39;_&#39;) }}&quot;

产生结果

arr:
- Octane
- 2022.1.0.6
- Houdini
- 19.5.493
- OSX.zip

选择满足 "是2位数字 + '.' + 1位数字" 条件的项目

version: &quot;{{ arr|select(&#39;regex&#39;, version_regex)|first }}&quot;
version_regex: &#39;^\d{2}\.\d{1}.*$&#39;

产生结果

version: 19.5.493

如果版本格式是语义化版本(major.minor.patch),拆分项目

major: &quot;{{ version|split(&#39;.&#39;)|first }}&quot;
major_minor: &quot;{{ version|splitext|first }}&quot;
patch: &quot;{{ version|split(&#39;.&#39;)|last }}&quot;

产生结果

major: 19
major_minor: 19.5
patch: 493

用于测试的完整Playbook示例

- hosts: localhost

  vars:

    string: Octane_2022.1.0.6_Houdini_19.5.493_OSX.zip
    arr: &quot;{{ string|split(&#39;_&#39;) }}&quot;
    version_regex: &#39;^\d{2}\.\d{1}.*$&#39;
    # version: &quot;{{ arr|select(&#39;regex&#39;, version_regex)|first }}&quot;
    version: &quot;{{ string|
                 split(&#39;_&#39;)|
                 select(&#39;regex&#39;, version_regex)|first }}&quot;
    major: &quot;{{ version|split(&#39;.&#39;)|first }}&quot;
    major_minor: &quot;{{ version|splitext|first }}&quot;
    patch: &quot;{{ version|split(&#39;.&#39;)|last }}&quot;
    # major_minor: &quot;{{ version|split(&#39;.&#39;)[:2]|join(&#39;.&#39;) }}&quot;

  tasks:

    - debug:
        var: arr
    - debug:
        var: version

    - debug:
        msg: |
          major: {{ major }}
          major_minor: {{ major_minor }}
          patch: {{ patch }}          


```

英文:

Declare the variables

  version_regex: &#39;^\d{2}\.\d{1}.*$&#39;
  version: &quot;{{ string|
               split(&#39;_&#39;)|
               select(&#39;regex&#39;, version_regex)|first }}&quot;
  major: &quot;{{ version|split(&#39;.&#39;)|first }}&quot;
  major_minor: &quot;{{ version|splitext|first }}&quot;
  patch: &quot;{{ version|split(&#39;.&#39;)|last }}&quot;

gives

  version: 19.5.493
  major: 19
  major_minor: 19.5
  patch: 493

<hr>

<sup>

Details:

Given the string

  string: Octane_2022.1.0.6_Houdini_19.5.493_OSX.zip

Split the string

  arr: &quot;{{ string|split(&#39;_&#39;) }}&quot;

gives

  arr:
  - Octane
  - 2022.1.0.6
  - Houdini
  - 19.5.493
  - OSX.zip

Select the item that "is 2 digits + '.' + 1 digit"

  version: &quot;{{ arr|select(&#39;regex&#39;, version_regex)|first }}&quot;
  version_regex: &#39;^\d{2}\.\d{1}.*$&#39;

gives

  version: 19.5.493

If the version's format is semantic versioning (major.minor.patch) split the items

  major: &quot;{{ version|split(&#39;.&#39;)|first }}&quot;
  major_minor: &quot;{{ version|splitext|first }}&quot;
  patch: &quot;{{ version|split(&#39;.&#39;)|last }}&quot;

gives

    major: 19
    major_minor: 19.5
    patch: 493

Example of a complete playbook for testing

- hosts: localhost

  vars:

    string: Octane_2022.1.0.6_Houdini_19.5.493_OSX.zip
    arr: &quot;{{ string|split(&#39;_&#39;) }}&quot;
    version_regex: &#39;^\d{2}\.\d{1}.*$&#39;
    # version: &quot;{{ arr|select(&#39;regex&#39;, version_regex)|first }}&quot;
    version: &quot;{{ string|
                 split(&#39;_&#39;)|
                 select(&#39;regex&#39;, version_regex)|first }}&quot;
    major: &quot;{{ version|split(&#39;.&#39;)|first }}&quot;
    major_minor: &quot;{{ version|splitext|first }}&quot;
    patch: &quot;{{ version|split(&#39;.&#39;)|last }}&quot;
    # major_minor: &quot;{{ version|split(&#39;.&#39;)[:2]|join(&#39;.&#39;) }}&quot;

  tasks:

    - debug:
        var: arr
    - debug:
        var: version

    - debug:
        msg: |
          major: {{ major }}
          major_minor: {{ major_minor }}
          patch: {{ patch }}          

</sup>

答案2

得分: 1

你可以使用这个正则表达式

Octane_(.*(?=\_Houdini))_Houdini_(.*(?=\_))

它将返回两个值的组

  • 年份: 2022.1.0.6
  • 版本: 19.5.493

这个正则表达式是如何工作的

(?=\_)

使用正向预查来匹配你想要的标识符,在这种情况下,第一个组是 _Houdini,第二个组是 _

英文:

you can use this regex

Octane_(.*(?=\_Houdini))_Houdini_(.*(?=\_))

it will return 2 groups of value

  • year: 2022.1.0.6
  • version: 19.5.493

how this regex works

(?=\_)

using positive lookahead to match the indentifier you want, for this case is _Houdini for 1st group and _ for 2nd group

答案3

得分: 1

We can work backwards by using a lookahead:

(?<=_)\d{2}\.\d(?=\.\d+_[A-Z]+\.zip)

This only matches if preceded by an underscore, then looks for exactly two digits followed by a ".", then by one digit, and it is only matched if proceeded by another dot, one or more digits, an underscore, several capital letters, then the literal ".zip".

Try it.

英文:

We can work backwards by using a lookahead:

(?&lt;=_)\d{2}\.\d(?=\.\d+_[A-Z]+\.zip)

This only matches if preceded by an underscore, then looks for exactly two digits followed by a &quot;.&quot;, then by one digit, and it is only matched if proceeded by another dot, one or more digits, an underscore, several capital letters, then the literal &quot;.zip&quot;.

Try it.

huangapple
  • 本文由 发表于 2023年3月1日 09:33:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定