简体   繁体   中英

python regex find multiple matches with findall

I need to find 2 regex patterns , but i am not finding away to make it work

i have a variables that could be one of those (just example the are more )

repository = "Red Hat Enterprise Linux Extended Update Support 7.2"

repository = "Red Hat Enterprise Linux 7"

i need to find both repositories with that patterns:

regex_rhel = 'Red Hat Enterprise Linux' + " " + str(major_version)

regex_eus = 'Red Hat Enterprise Linux Extended Update Support' + " " + str(rhel_ver)

i have tried with findall ,but getting:

eus_latest_repos = re.findall(r'"Red Hat Enterprise Linux' + " " + str(major_version)|('Red Hat Enterprise Linux Extended Update Support' + " " + str(rhel_ver),repository))
                    print(eus_latest_repos)


TypeError: unsupported operand type(s) for |: 'str' and 'tuple'

If I understood you need a regex to extract the repo from a string.

Demo:

import re
repository = "Red Hat Enterprise Linux Extended Update Support 7.2"
repository = "Red Hat Enterprise Linux 7"


s = "sadasdjkasdflsdfsdf;ljk lsjdlsdfj Red Hat Enterprise Linux Extended Update Support 7.2 kjshdklsdhksdjf sdfdfdfgdfg Red Hat Enterprise Linux 7 sdfsdfkl;dfjsgsdfg adfadsfladjksfds"


print(re.findall("Red Hat Enterprise Linux Extended Update Support\s+\d*\.?\d+|Red Hat Enterprise Linux\s+\d+", s))

Output:

['Red Hat Enterprise Linux Extended Update Support 7.2', 'Red Hat Enterprise Linux 7']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM