如何在正则表达式中匹配特定长度字符的字符串? (Python)
How to match strings of only a specific length of characters in regex? (Python)
我正在使用
\[(.*?)\]|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.
但我只希望它匹配日期,而不是括号之间的其他内容以及分配命名组 'code'(部分工作)。我尝试了几种变体,包括
\[(\d*?)\]
\[(\W*?)\]
\[^(\.*?){23}$\]
但我似乎无法找到符合这些标准的任何内容。
奖金:
一旦剩下的问题解决了,我也许就能解决这个问题,但我不妨在我在这里的时候问问。如何使用日期和代码作为键值对更新字典?
正则表达式:\d{4}(?:-\d{2}){2}[^]]+|(?<=Response code )(?P<code>\d+)
详情:
(?:)
非捕获组
{n}
完全匹配 n
次
[^]
命名捕获组
|
或
(?<=)
正面回顾
(?P<>)
命名捕获组
Python代码:
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
我正在使用
\[(.*?)\]|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.
但我只希望它匹配日期,而不是括号之间的其他内容以及分配命名组 'code'(部分工作)。我尝试了几种变体,包括
\[(\d*?)\]
\[(\W*?)\]
\[^(\.*?){23}$\]
但我似乎无法找到符合这些标准的任何内容。
奖金: 一旦剩下的问题解决了,我也许就能解决这个问题,但我不妨在我在这里的时候问问。如何使用日期和代码作为键值对更新字典?
正则表达式:\d{4}(?:-\d{2}){2}[^]]+|(?<=Response code )(?P<code>\d+)
详情:
(?:)
非捕获组{n}
完全匹配n
次[^]
命名捕获组|
或(?<=)
正面回顾(?P<>)
命名捕获组
Python代码:
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