douwu8524 2011-11-02 11:30
浏览 29
已采纳

PHP正则表达式:无法识别下一个子模式是否启动另一个模式

I've been trying to extract this data from a file but the thing is, at the point where I'm stuck, there could be a whole new pattern (that starts with a date), or there could be a complemente in the route (which does not start with a digit).

I'm having trouble identifying whether or not the next digit is a new pattern or a complement. I also haven't been able to optimize this pattern, as you can see after the EQPT mark.

Examples of strings to match:

291011 311011 1234560 AZU4059 E190/M SBKP1513 N0458 350 DCT BGC DCT TRIVI DCT CNF UW58      SBRF0249 EQPT/WRG PBN/D1O1 EET/SBRE0107 SAGAZ/N0454F370 UW58 GEBIT UW10

271011 UFN    1230060 AZU4062 E190/M SBPA2140 N0460 350 UM540 OSAMU DCT NEGUS UW47          SBKP0120 EQPT/WRG PBN/D1O1 EET/SBBS0106

My regex so far:

preg_match_all('/([0-3][0-9][0|1][0-9][0-9]{2})\s*(UFN|[0-3][0-9][0|1][0-9][0-9]{2})\s*([0-7]{7})\s*(AZU[0-9]{4})\s*([A-Z0-9]{4})\/([L|M|H])\s*([A-Z0-9]{8})\s*(N[0-9]{4})\s*([0-9]{3})\s*([\S\s]{1,40})\s*([A-Z0-9]{8})\s*(EQPT\/WR?G?\s?P?B?N?\/?D?1?O?1?\s?E?E?T?\/?([A-Z0-9]{8})?)\s*)/', $result, $match);
  • 写回答

1条回答 默认 最新

  • doupo1890 2011-11-05 10:28
    关注

    I got it!

    I had to do many things to make this work:

    I removed all the blank double spaces and replaced all of the first sub-pattern dates by "######". I also replaced and second parameters by "UFN" and mapped the ones I replace with a couple of arrays.

    Then I added a # at the end and used it at the end of the regex pattern, so that it would be certain that it would start a new pattern when it came to a #. And it all worked out, I then just had to reposition the rest of the route so that it would complement the other one.

    Thank you for trying to help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式