简体   繁体   English

如何使用OR进行正则表达式

[英]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 基于设备有两个输入,它需要使用Or匹配任何输入,并给出版本输出,如7.0.3I4.6或7.1.4.N1.1。

Input-1 NXOS image file is: bootflash:///nxos.7.0.3.I4.6.bin Input-1 NXOS映像文件是: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 输入2 kickstart映像文件是:bootflash:///n6000-uk9-kickstart.7.1.4.N1.1.bin系统映像文件是:bootflash:///n6000-uk9.7.1.4.N1.1。箱子

Please suggest how to filter using Or and regular expression 请建议如何使用Or和正则表达式进行过滤

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)')] 输出:[('NXOS:版本7.0(3)I4(6)','7.0(3)I4(6)','','')] [('',``,'kickstart:版本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 如何根据输入过滤7.0(3)I4(6)和7.1(4)N1(1)

This is really simple with the | 使用|这真的很简单| (OR) operator: (OR)运算符:

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 关键是OR运算符介于两个术语之间,没有定界括号或括号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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