正则表达式以匹配相似词后的字符串

时间:2019-02-27 16:52:26

标签: php regex pcre

我正在尝试解析下面的日志文件;但是,在所有“ Step = Number”之后,我仍然想弄清楚如何匹配结果。

  

Step = 10,Step = 11,Step = 12,Step = 13,Step = 14,Step = 15,Step = 16,Step = 18,Step = 17,Step = 20,Step = 19,Step = 25 ,步骤= 21,步骤= 26,步骤= 28,步骤= 24,步骤= 22,步骤= 23,步骤= 27,步骤= 30,步骤= 29,步骤= 35,步骤= 34,步骤= 32,步骤= 31,步骤= 38,步骤= 37,步骤= 36,步骤= 50,步骤= 45,步骤= 48,步骤= 41,步骤= 52,步骤= 42,步骤= 57,步骤= 65,步骤= 61 ,步骤= 62,步骤= 64,步骤= 54,步骤= 53,步骤= 59,步骤= 63,步骤= 84,步骤= 71,SelectedAuthenticationIdentityStores = pappedvalue,NetworkDeviceName = exampledevice,NetworkDeviceGroups = Update Source:All Sources:ACS ,NetworkDeviceGroups =设备类型:所有设备类型:无线,NetworkDeviceGroups =位置:所有位置,ServiceSelectionMatchedRule = Rule-1,IdentityPolicyMatchedRule =默认

我正在考虑将\ d \ s \,\ s

组合后进行匹配

理想的目标是匹配以下内容:

  

SelectedAuthenticationIdentityStores =附加值,NetworkDeviceName =示例设备,NetworkDeviceGroups =更新源:所有源:ACS,NetworkDeviceGroups =设备类型:所有设备类型:无线,NetworkDeviceGroups =位置:所有位置,ServiceSelectionMatchedRule = Rule-1,IdentityPolicyMatchedRule =默认

我尝试了以下正则表达式:\d\s\\,\s(.*),但与第一个Step = Number(Step = 10)之后的所有内容都匹配

2 个答案:

答案 0 :(得分:1)

您可以在现有模式的开头使用另一个。*来贪婪地消耗除最后一个匹配项以外的所有内容:

.*\d\s,\s(.*)

演示:https://regex101.com/r/Oc7jUK/1

或者,您可以使用正向后看模式来确保匹配前面有\d\s,\s,并可以使用负向前瞻模式来确保后面没有\d\s,\s

(?<=\d\s,\s)(?!.*\d\s,\s).*

演示:https://regex101.com/r/Oc7jUK/2

答案 1 :(得分:0)

为什么不使用与SelectedAuthenticationIdentityStoresSelectedAuthenticationIdentityStores.*之类的\w{5,}.*之后都匹配的正则表达式

const regex = /SelectedAuthenticationIdentityStores.*/g;
const text = `Step=10 , Step=11 , Step=12 , Step=13 , Step=14 , Step=15 , Step=16 , Step=18 , Step=17 , Step=20 , Step=19 , Step=25 , Step=21 , Step=26 , Step=28 , Step=24 , Step=22 , Step=23 , Step=27 , Step=30 , Step=29 , Step=35 , Step=34 , Step=32 , Step=31 , Step=38 , Step=37 , Step=36 , Step=50 , Step=45 , Step=48 , Step=41 , Step=52 , Step=42 , Step=57 , Step=65 , Step=61 , Step=62 , Step=64 , Step=54 , Step=53 , Step=59 , Step=63 , Step=84 , Step=71 , SelectedAuthenticationIdentityStores=paddedvalue, NetworkDeviceName=exampledevice, NetworkDeviceGroups=Update Source:All Sources:ACS, NetworkDeviceGroups=Device Type:All Device Types:Wireless, NetworkDeviceGroups=Location:All Locations, ServiceSelectionMatchedRule=Rule-1, IdentityPolicyMatchedRule=Default`
console.log(text.match(regex))