c# - 在以以下开头的同一行中查找多个正则表达式模式

标签 c# regex

我搜索了很多类似的问题来解决这个问题,但在 Stackoverflow 中找不到任何可以解决该问题的帖子。

我试图仅在以“服务 ID”开头的行中查找模式的所有实例,但它也匹配其他行中的许多其他结果,我不想从任何其他地方包含这些结果。

这是输入文本:

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 :<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93e3fff2fdfdf6f7bdf2f0e7fae5fae7ead3c4ebeae9f0fcfefee6fdfaf0f2e7fafcfde0bdf0fcfe" rel="noreferrer noopener nofollow">[email protected]</a>
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

这是正则表达式:

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

这是预期的输出:

066BANGX1234567890
066BANGX1234567891

我在 Regex101.com 上有一份 RegEx 副本

最佳答案

您可以将此正则表达式与\G一起使用对于您的比赛:

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

RegEx Demo

正则表达式详细信息:

  • (?:^Service ID|(?!^)\G,) :匹配以 Service ID 开头的行或上一场比赛结束
  • \G断言前一个匹配的末尾位置或第一个匹配的字符串开头以及负向前瞻 (?!^)确保我们不匹配 \G在行开始处
  • \h+ : 匹配 1+ 个空格
  • (?<CircuitID>[A-Z0-9]{11,30}) :匹配您的CircuitID在名为 CircuitID 的捕获组中即 11 到 30 个字母数字字符

关于c# - 在以以下开头的同一行中查找多个正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64781308/

相关文章:

java - 元音的正则表达式负向前瞻

Python Regex - 获取匹配词

php - 基于内容的不同替换

MySQL RegEx匹配两个相同的连续数字

c# - 使用 Expression Blend SDK 行为破坏 Visual Studio 设计器

c# - 当前的 VS 工具可以让每个开发人员以不同的方式查看 C# 代码

c# - 如何监听 ListView 中的滚动?

c# - 创建 HttpClient 后,我​​可以更改 HttpClientHandler 的属性吗?

c# - 如何用正则表达式匹配一个特定的句子

javascript - 使用 javascript 或正则表达式从值中分割数字和字符串