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+)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧