I have am writing a regular expression to return the version number of OpenSSH installed on a Windows machine, for our monitoring system. I have one of two strings:
version=OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.4
version=OpenSSH_7.1p1 Microsoft_Win32_port_with_VS Dec 22 2015, OpenSSL 1.0.2d 9 Jul 2015
When the regex is:
\S+Windows_(\d.\d)
Then 7.7 is in group 1 and the monitoring system sees it. But when I try to cover the 7.1 string, the grouping gets messed up.
(\S+Windows_(\d.\d)|\S+OpenSSH_(\d.\d))
How can I modify that string to isolate group 3 and group 1 (for 7.1 and 7.7 respectively)?
Thanks.
You might consider changing the regex entirely so you only have one capture group.
Both digits you are trying to capture start with version=OpenSSH_ with some optional characters in the middle.
Therefore you can do:
version=OpenSSH_\D*(\d\.\d)
Which will capture the correct version in either case. The advantage is you do not need to know which match group to use -- the return is always group 1.
Demo
If you want to use the alteration form that you have, that can be refactored a bit as well to have a single capture group:
(?:Windows_|\S+OpenSSH_)(\d.\d)
Demo
Just know that format will have much more backtracking and may be 10x less efficient than the first form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With