【问题标题】:Regex to match string after similar words正则表达式匹配相似词后的字符串
【发布时间】:2019-07-21 11:27:27
【问题描述】:

我正在尝试解析下面的日志文件;但是,在所有“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 , 步=38 , 步=37 , 步=36 , 步=50 , 步=45 , 步=48 , 步=41 , 步=52 , 步=42 , 步=57 , 步=65 , 步=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=Default

我在考虑组合后的匹配:\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

p>

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

【问题讨论】:

  • (?: ?Step=\d+ ,)*\s(.*) 好吗?
  • /(Step=\d+\s*,\s*)+(.*)/ 应该这样做。 \2 会给你想要的字符串。
  • 如何将(Step=\d+\s,\s) 模式一无所有,这样你就可以轻松获取字符串的最后一部分?

标签: php regex pcre


【解决方案1】:

为什么不使用匹配所有 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))

【讨论】:

  • 我只是想给出一个直观的例子
  • “SelectedAuthenticationIdentityStores”属性不一定出现在“Step=Number”之后。因此,我无法在特定单词上创建静态匹配。感谢贡献@G.aziz
【解决方案2】:

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

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

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

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

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

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

【讨论】:

  • 我喜欢这个答案,是否可以减少正则表达式引擎消耗的步骤数?
  • 我已经更新了我的答案,以准确匹配您当时所寻找的内容。
  • 我认为您的初始解决方案在正则表达式计算时间和准确性方面是最好的。 .*\d\s,\s(.*)
  • 我也这么认为。仅在您要求后才进行第二个解决方案。我已经用包含的第一个解决方案更新了答案。
猜你喜欢
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 2011-09-02
  • 2011-12-13
  • 2012-07-19
  • 2013-10-12
  • 2012-06-05
  • 2013-12-25
相关资源
最近更新 更多