Regex 如何在正则表达式中只匹配特定长度字符的字符串?(Python)
我正在使用Regex 如何在正则表达式中只匹配特定长度字符的字符串?(Python),regex,python-3.x,regex-group,Regex,Python 3.x,Regex Group,我正在使用 \[(.*?)\]|Response code (?P<code>\d+) 要搜索这些字段,请执行以下操作: [2018-01-20 05:19:54.812] INFO com.mulesoft.ch.monitoring.MonitoringCoreExtension [qtp689806602-32]: Monitoring enabled: true [2018-01-20 05:19:54.813] INFO com.mulesoft.ch.mon
\[(.*?)\]|Response code (?P<code>\d+)
要搜索这些字段,请执行以下操作:
[2018-01-20 05:19:54.812] INFO com.mulesoft.ch.monitoring.MonitoringCoreExtension [qtp689806602-32]: Monitoring enabled: true
[2018-01-20 05:19:54.813] INFO com.mulesoft.ch.monitoring.MonitoringCoreExtension [qtp689806602-32]: Registering ping flow injector...
[2018-01-20 05:19:54.833] INFO com.mulesoft.ch.queue.boot.PersistentQueueCoreExtension [qtp689806602-32]: The PersistentQueueManager is NOT configured. The normal VM queue manager will be used.
[2018-01-20 05:19:54.841] INFO org.mule.lifecycle.AbstractLifecycleManager [qtp689806602-32]: Initialising RegistryBroker
[2018-01-20 05:19:54.872] INFO
[2018-01-24 02:14:30.153] INFO org.mule.routing.SynchronousUntilSuccessfulProcessingStrategy [[swt-fastsalescomp-anaplan-schedules].ScatterGatherWorkManager.24]: Exception thrown inside until-successful org.mule.module.http.internal.request.ResponseValidatorException: Response code 503 mapped as failure.
但我只希望它与日期匹配,而不是括号中的其他内容,以及指定一个命名组“代码”(这部分工作正常)。我尝试了几种变体,包括
\[(\d*?)\]
\[(\W*?)\]
\[^(\.*?){23}$\]
但我似乎找不到符合这些标准的东西
奖金:
一旦其余的问题解决了,我也许能解决这个问题,但我还是在这里问问吧。如何将日期和代码作为键值对更新字典?Regex:
详情:
非捕获组(?:)
精确匹配{n}
次n
命名捕获组[^]
或|
(?仅日期,而非时间?请参见。@WiktorStribiżew您可以为正则表达式的第一部分添加一个
,以强制它从行的开头开始作为时间戳!!!否则很好;-)作为后续操作,我如何仅返回带有503的时间值?@Cdhippen您是指02:14:30.153?是的。我的日志有上千个时间条目,我正在尝试只提取带有时间和状态代码的行。将其更改为:现在我完全得到了我想要的(我不需要毫秒,这将使事情变得复杂,因为我将尝试将其放入matplotlib中),非常感谢您的帮助!^
for match in re.finditer(r'\d{4}(?:-\d{2}){2}[^]]+|(?<=Response code )(?P<code>\d+)', text): print(match.group())
2018-01-20 05:19:54.812 2018-01-20 05:19:54.813 2018-01-20 05:19:54.833 2018-01-20 05:19:54.841 2018-01-20 05:19:54.872 2018-01-24 02:14:30.153 503