I am trying to use ansible regex_search to get the value in ansible tasks.
Full string:
VRP (R) software, Version 5.170 (S5735 V200R019C00SPC500)
String that I want to match:
V200R019C00SPC500
So I am trying to match the second parenthesis , second fields after a space.
This assume the value is always different hence I will not try to match V200 or any of the string.
I have tried a few methods but all is not working, kindly advise which one is better and more accurate.
Here is the regex101.
As you can see the string i wanted to match is in "Match 2".
However when translate into ansible it will only print the first match, i.e (R)
ansible tasks:
- debug:
msg:
- "{{ displayversion |regex_search('[(].*?.[)]') }}"
- "{{ displayversion |regex_search('\\(([^)]+)?([^)])\\)') }}"
- "{{ displayversion |regex_search('^(.+)\\s(.+)', '\\2') }}"
vars:
displayversion: VRP (R) software, Version 5.170 (S5735 V200R019C00SPC500)
will get
ok: [localhost] => {
"msg": [
"(R)",
"(R)",
[
"V200R019C00SPC500)"
]
]
}
The 3rd options is the closest one, but it still have the parenthesis and is a list , I would also like to know how to remove it within the same regex.
Thanks