SavindraSingh
SavindraSingh

Reputation: 961

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.

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.

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 :[email protected]
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:

066BANGX1234567890
066BANGX1234567891

I have a copy of RegEx available at Regex101.com

Upvotes: 2

Views: 356

Answers (1)

anubhava
anubhava

Reputation: 785196

You may use this regex with \G for your matches:

(?:^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
  • \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
  • \h+: Match 1+ whitespace
  • (?<CircuitID>[A-Z0-9]{11,30}): Match your CircuitID in a captured group named as CircuitID that is 11 to 30 of alphanumeric characters

Upvotes: 2

Related Questions