英文:
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('[0-9]{2}.[0-9]')
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: '^\d{2}\.\d{1}.*$'
version: "{{ string|
split('_')|
select('regex', version_regex)|first }}"
major: "{{ version|split('.')|first }}"
major_minor: "{{ version|splitext|first }}"
patch: "{{ version|split('.')|last }}"
产生结果
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: "{{ string|split('_') }}"
产生结果
arr:
- Octane
- 2022.1.0.6
- Houdini
- 19.5.493
- OSX.zip
选择满足 "是2位数字 + '.' + 1位数字" 条件的项目
version: "{{ arr|select('regex', version_regex)|first }}"
version_regex: '^\d{2}\.\d{1}.*$'
产生结果
version: 19.5.493
如果版本格式是语义化版本(major.minor.patch),拆分项目
major: "{{ version|split('.')|first }}"
major_minor: "{{ version|splitext|first }}"
patch: "{{ version|split('.')|last }}"
产生结果
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: "{{ string|split('_') }}"
version_regex: '^\d{2}\.\d{1}.*$'
# version: "{{ arr|select('regex', version_regex)|first }}"
version: "{{ string|
split('_')|
select('regex', version_regex)|first }}"
major: "{{ version|split('.')|first }}"
major_minor: "{{ version|splitext|first }}"
patch: "{{ version|split('.')|last }}"
# major_minor: "{{ version|split('.')[:2]|join('.') }}"
tasks:
- debug:
var: arr
- debug:
var: version
- debug:
msg: |
major: {{ major }}
major_minor: {{ major_minor }}
patch: {{ patch }}
```
英文:
Declare the variables
version_regex: '^\d{2}\.\d{1}.*$'
version: "{{ string|
split('_')|
select('regex', version_regex)|first }}"
major: "{{ version|split('.')|first }}"
major_minor: "{{ version|splitext|first }}"
patch: "{{ version|split('.')|last }}"
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: "{{ string|split('_') }}"
gives
arr:
- Octane
- 2022.1.0.6
- Houdini
- 19.5.493
- OSX.zip
Select the item that "is 2 digits + '.' + 1 digit"
version: "{{ arr|select('regex', version_regex)|first }}"
version_regex: '^\d{2}\.\d{1}.*$'
gives
version: 19.5.493
If the version's format is semantic versioning (major.minor.patch) split the items
major: "{{ version|split('.')|first }}"
major_minor: "{{ version|splitext|first }}"
patch: "{{ version|split('.')|last }}"
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: "{{ string|split('_') }}"
version_regex: '^\d{2}\.\d{1}.*$'
# version: "{{ arr|select('regex', version_regex)|first }}"
version: "{{ string|
split('_')|
select('regex', version_regex)|first }}"
major: "{{ version|split('.')|first }}"
major_minor: "{{ version|splitext|first }}"
patch: "{{ version|split('.')|last }}"
# major_minor: "{{ version|split('.')[:2]|join('.') }}"
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".
英文:
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"
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论