how to extract minor version from major version variable

167 views
Skip to first unread message

Sameer Modak

unread,
Oct 25, 2023, 8:46:34 AM10/25/23
to Ansible Project
Hello Team,

What is the correct way to extract just major version from full version like ,

full version is : 2.8.3
need version: 2.8

do we use split or regex 

Sameer Modak

unread,
Oct 25, 2023, 8:51:09 AM10/25/23
to Ansible Project

 i tried with  regex_replace('^([0-9])\\.([0-9]*).*', '\\1.\\2')

but is there any other better way to do it

Todd Lewis

unread,
Oct 25, 2023, 11:14:40 AM10/25/23
to ansible...@googlegroups.com, uto...@gmail.com
If you can manage to express your problem in terms of a test, then you can use Ansible's version test: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/version_test.html

Otherwise, Ansible's own utils/version.py (which is used by the version test) uses the following regex. Maybe that will inspire you.

# Regular expression taken from
# https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
SEMVER_RE = re.compile(
    r'''
    ^
        (?P<major>0|[1-9]\d*)
        \.
        (?P<minor>0|[1-9]\d*)
        \.
        (?P<patch>0|[1-9]\d*)
        (?:
            -
            (?P<prerelease>
                (?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)
                (?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*
            )
        )?
        (?:
            \+
            (?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)
        )?
    $
    ''',
    flags=re.X
)
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7f71e811-5c05-483b-be5b-f50261860d82n%40googlegroups.com.

-- 
Todd
Reply all
Reply to author
Forward
0 new messages