duanmianzhou5353 2019-05-01 07:40
浏览 56
已采纳

如何迭代“1或多”项的正则表达式?

I am trying to develop a regular expression to read a pager message into categories. At the end are the responding brigades' codes (CBORT, CYAND)

These brigade codes represent each responding brigade. The issue is that there can either be one eg. (CBORT) or many eg. (CBAGR, CBORT, CYAND). I am unsure how to make the regex match each brigade as an individual match.

Each brigade code will have the letter C as a prefix.

Can this be done using a regular expression or will I require a PHP script to iterate through the last part of the message to match each of these brigade codes into an array?

Pager Message: @@ALERT BORT1 G&SC1 GRASS FIRE - SPREADING QUICKLY 79 BOORT-YANDO RD BOORT SVNW 214 J15 (475017) AFPR CBORT CYAND F190400036

Current Regex: (@@)(ALERT)\s(\w+)\s(\S+)(C1|C3)\s(.+)\s(\d+|\d+KM|\d+ M|CNR|NEAR|NEXT TO|ADJACENT|BEHIND|ACROSS FROM|ACROSS|REAR OF|REAR|OUTSIDE)\s(.+)\s(SVNW)\s(\d+)\s(\w\d+)\s((\d+))\s(F|AF|FP|AFP|AFPR|AFPRS)\s(C\w+)\s(F\d+)

The bold section is the section I wish to iterate 1 or many times.

Thank you

  • 写回答

1条回答 默认 最新

  • dsolwotv00116 2019-05-01 07:52
    关注

    You can use ((C\w+)\s)+ to have at least one group that match, but capture the following ones as well.

    You also need to escape the parenthesis of your message with \( and \) (reserved characters)

    Full regex :

    (@@)(ALERT)\s(\w+)\s(\S+)(C1|C3)\s(.+)\s(\d+|\d+KM|\d+ M|CNR|NEAR|NEXT TO|ADJACENT|BEHIND|ACROSS FROM|ACROSS|REAR OF|REAR|OUTSIDE)\s(.+)\s(SVNW)\s(\d+)\s(\w\d+)\s(\(\d+\))\s(F|AF|FP|AFP|AFPR|AFPRS)\s((C\w+)\s)+(F\d+)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法