简体   繁体   中英

How to do regular expressions with OR

Had two inputs based on the device it need to match any of input using Or and give the version output like 7.0.3I4.6 or 7.1.4.N1.1

Input-1 NXOS image file is: bootflash:///nxos.7.0.3.I4.6.bin

Input-2 kickstart image file is: bootflash:///n6000-uk9-kickstart.7.1.4.N1.1.bin system image file is: bootflash:///n6000-uk9.7.1.4.N1.1.bin

Please suggest how to filter using Or and regular expression

Tried

regex_version = re.compile(r'(NXOS:\sversion\s(\S+))|(kickstart:\sversion\s(\S+))')

version = regex_version.findall(input)

Tried

regex_version = re.compile(r'(NXOS:\sversion\s(\S+))|(kickstart:\sversion\s(\S+))')

version = regex_version.findall(input)

output: [('NXOS: version 7.0(3)I4(6)', '7.0(3)I4(6)', '', '')] [('', '', 'kickstart: version 7.1(4)N1(1)', '7.1(4)N1(1)')]

how to filter 7.0(3)I4(6) and 7.1(4)N1(1) based on the input

This is really simple with the | (OR) operator:

regex_version = re.compile(r'(NXOS:\sversion\s(\S+)|kickstart:\sversion\s(\S+))')

The key is that the OR operator goes between the two terms with no delimiting parentheses or brackets

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