简体   繁体   English

正则表达式到Python上的单词匹配

[英]Regex to word match on python

I'm trying to create a regex for extracting the software name and the version (if exist) from a Nmap output for example : 我正在尝试创建一个正则表达式,例如从Nmap输出中提取软件名称和版本(如果存在):

ftp (product: vsftpd version: 2.3.4 ostype: Unix)

I applied the regex : /product: (.*) version: (.*)/ 我应用了正则表达式: /product: (.*) version: (.*)/

It gives the result : 结果如下:

group(1) = vsftpd ,  group(2) = 2.3.4 ostype: Unix 

while I only need the version (2.3.4) 而我只需要版本(2.3.4)

Add to that the version should be optional I tried different things but it doesn't work Example : 另外,该版本应该是可选的,我尝试了其他方法,但是它不起作用示例:

product: Apache Jserv extrainfo: Protocol v1.

As there is no version I only need the part of `Apache Jserv ' 由于没有版本,我只需要`Apache Jserv'的一部分

How do i make all of this ? 我该怎么做?

使用此正则表达式演示

product: ([\w ]+)(?:version: ([\w.]+)| \w+:)

只需忽略空格即可解决您的第一个问题,然后在没有空格的情况下使用Alternate不返回版本。

(?<=product|version):\s(\S*)

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

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