首页 > 解决方案 > 2 提取构建号和版本名称的正则表达式

问题描述

我真的很难理解正则表达式。

我有这个字符串:

Windows SERVERMAIN 10.0.14393 Microsoft Windows Server 2016 Standard x64

我需要创建两个正则表达式。

第一个应该在第二个空格之后和第三个空格之前返回字符串的一部分,所以我剩下:

10.0.14393

第二个应该返回第三个空格之后的所有内容,所以我剩下:

Microsoft Windows Server 2016 Standard x64

有没有人能帮我解决这个问题,到目前为止我只能使用:

\s+\w+\s(.*)

这给了我:

SERVERMAIN 10.0.14393 Microsoft Windows Server 2016 Standard x64

更新 1 在@rock321987 的帮助下,我回顾了我想如何实现它。

我现在有这个字符串:

Microsoft Windows Server 2016 Datacenter x64 - 10.0.14393

我想分成两组:

Microsoft Windows Server 2016 Datacenter x64

10.0.14393

标签: regexpcre

解决方案


正则表达式 1

^.*?[ ]+.*?[ ]+(.*?)[ ]

正则表达式 2

^.*?[ ]+.*?[ ]+.*?[ ]+(.*)$

正则表达式 1 细分

^ #Start of string
.*?[ ]+ #Match till 1st space
.*?[ ]+ #Match till 2nd space
(.*?)[ ]+ #Capture the match after 2nd space till 3rd space

正则表达式 2 细分

^.*?[ ]+.*?[ ]+.*?[ ]+ #Explanation same as above. Match till 3rd space
(.*)$ #Match everything after 3rd space till last

编辑:如果您的工具允许,这也可以在单个正则表达式中完成

^.*?[ ]+.*?[ ]+(.*?)[ ]+(.*)$

编辑1:如果你愿意,你也可以\K使用

^.*?[ ]+.*?[ ]+\K([^ ]+)

推荐阅读