regex - 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
解决方案
^.*?[ ]+.*?[ ]+(.*?)[ ]
^.*?[ ]+.*?[ ]+.*?[ ]+(.*)$
正则表达式 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([^ ]+)
推荐阅读
- node.js - 使用 MongoDB 的 Express 应用程序无法在 Heroku 上运行
- javascript - 打字稿编译供应商,排除不会工作
- java - 项目缺少所需的库 - 缺少 jar 文件
- android - 无法读取未定义异常 vue-native 的属性“transformFile”
- c - 在C中关闭部分套接字
- python-jira - 使用 WIKI 格式插入注释的 Jira Python API
- reactjs - React js, redux 消费 api
- prettier - 更漂亮 - 在其他之前保留换行符
- c# - 你能告诉我修改此代码以读取 csv 文件中的下一个单元格吗
- html - 调整 svg 图像的大小