簡體   English   中英

正則表達式匹配類似單詞后的字符串

[英]Regex to match string after similar words

我試圖解析下面的日志文件; 但是,在所有“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,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 =設備類型:所有設備類型:無線,NetworkDeviceGroups =位置:所有位置,ServiceSelectionMatchedRule = Rule-1,IdentityPolicyMatchedRule =默認

在以下組合之后我考慮匹配:\\ d \\ s \\,\\ s

理想的目標是匹配以下內容:

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

我嘗試了下面的正則表達式: \\d\\s\\\\,\\s(.*)但是匹配了第一步后的所有步驟=數字(步驟= 10)

您可以在現有模式的開頭使用另一個。*來貪婪地使用除最后一個匹配之外的所有匹配:

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

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

或者,您可以使用正向lookbehind模式來確保匹配前面有\\d\\s,\\s和負前瞻模式,以確保后面不再有\\d\\s,\\s

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

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

為什么不使用與SelectedAuthenticationIdentityStores類的所有匹配的正則表達式,如SelectedAuthenticationIdentityStores.*\\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)) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM