简体   繁体   English

在以以下开头的同一行中查找多个 RegEx 模式

[英]Find multiple RegEx patterns in the same line that begins with

I have searched a lot for similar questions to solve this issue but couldn't find any post in Stackoverflow that can solve the issue.我已经搜索了很多类似的问题来解决这个问题,但在 Stackoverflow 中找不到任何可以解决这个问题的帖子。

I am trying to find all instances of a pattern ONLY in a line that begins with 'Service ID' but it is also matching many other results from other lines, that I don't want to include from any other places.我试图仅在以“服务 ID”开头的行中查找模式的所有实例,但它也匹配其他行的许多其他结果,我不想从任何其他地方包含这些结果。

Here is the input text:这是输入文本:

Dear Customer,
 
Wxyz Communication is in receipt of maintenance activity. The details of maintenance activity are as below. Your below mentioned service would experience an outage of “2 Hours” during the activity window.
 
Wxyz COMMUNICATIONS  

 Planned Work Notification
Ticket Reference - TCL  CHGP1234567
Service ID  066BANGX1234567890, 066BANGX1234567891, A0B1C-D4E-FG5
NIMS ID A0ANX-T9Y-NP1, A0BC5-T1Y-NP1
Maintenance Type    Emergency
Activity Status Scheduled
Execution Owner 3rd party service Provider
Location of activity    Thailand 
 
Activity Window (IST)   2020-09-07 23:30:00 IST to 2020-09-08 04:30:00 IST
Activity Window (GMT)   2020-09-07 18:00:00 GMT to 2020-09-07 23:00:00 GMT
Expected Impact Duration(DD:HH:MM) :    2 Hours
 
Activity Description    Service provider will be performing an emergency software up gradation on their core router to improve the performance of service.
We request you to reschedule sensitive operations at your end accordingly.
We apologize for any inconvenience due to this event and resulting downtime.
If you have any queries with respect to this activity, feel free to contact us on the coordinates mentioned below:
Mail ID :planned.activity@Wxyzcommunications.com
Contact Number : + 91 20 1234 5678 & Toll Free no: 1-8001234567
We look forward to your co-operation and a long term synergic association.
Best Regards,
Customer Service
Wxyz COMMUNICATIONS
 
 
Ref:MSGTCL000027382595

Here is the RegEx:这是正则表达式:

(Service ID\s+,)*(?<CircuitID>[A-Z0-9]{11,30})

Here is the expected output:这是预期的 output:

066BANGX1234567890
066BANGX1234567891

I have a copy of RegEx available at Regex101.com我在Regex101.com有一份 RegEx

You may use this regex with \G for your matches:您可以将此正则表达式与\G一起用于匹配:

(?:^Service ID|(?!^)\G,)\h+(?<CircuitID>[A-Z0-9]{11,30})

RegEx Demo正则表达式演示

RegEx Details:正则表达式详细信息:

  • (?:^Service ID|(?,^)\G,) : Match a line starting with Service ID or end of the previous match (?:^Service ID|(?,^)\G,) :匹配以Service ID开头或上一个匹配结束的行
  • \G asserts position at the end of the previous match or the start of the string for the first match and negative lookahead (?!^) ensures that we don't match \G at line start \G在上一场比赛的结尾或第一场比赛的字符串开头断言 position 并且否定前瞻(?!^)确保我们在行开始时不匹配\G
  • \h+ : Match 1+ whitespace \h+ :匹配 1+ 个空格
  • (?<CircuitID>[A-Z0-9]{11,30}) : Match your CircuitID in a captured group named as CircuitID that is 11 to 30 of alphanumeric characters (?<CircuitID>[A-Z0-9]{11,30}) :在名为CircuitID的捕获组中匹配您的 CircuitID,该组CircuitID 11 到 30 个字母数字字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM