问题描述
我正在使用正则表达式来提取第 5 个和第 6 个字符串之间的字符串。我需要在没有空格的情况下提取它,因为冒号之间有空格。以下是我必须从中提取的示例
Abc Cloud : Xyz : Windows : Non Prod : Silver : ATC123XYZ : AQW 服务关闭
我尝试过 : (.+?):
但它返回: Xyz :,: Non Prod : 和 : ATC123XYZ :。我想要的只是ATC123XYZ。
解决方法
你可以试试:
^(?:[^:]+\s*:\s*){5}([^:\s]+)
Demo
说明:
^ from the start of the input
(?: match (but don't capture)
[^:]+\s*:\s* any term followed by :
){5} match exactly 5 of these
([^:\s]+) then match AND capture the 6th term
您想要的术语将在第一个捕获组中可用。