drcmue4619 2017-01-17 04:03
浏览 75

RegEx匹配PHP

Data:

N15319542045C13_1_3/61488007C13-130083_1_3/61488007C13-130083-1_1_3/P1197443641_1_3SD|1
NP1196939393_1_3SU|OD=2/7;|BNP1196939393_1_3SU|OD=2/7;|BNP1196930222_1_3SU|OD=4/11;|
NP1196930222_1_3SU|OD=4/11;|
N15319384625C13_1_3/61445794C13-130077_1_3SD||BN15319384625C13_1_3/61445794C13-130077_1_3SD||

RegExp:

(N(.*?)S([UID])\|(.*?))(?:B|\|.?$)

I am trying to find 7 matches using above regex but only 6 are matching. Not sure how to fix to match 1st line as well.

Format:

N(key)S(action)|(value or end)

end depend on different matches

I solved it if someone else needs:

(\x15(.*?)\x01([UID])\|(.*?))(?:.*?\x08|.*\|?$)
  • 写回答

1条回答 默认 最新

  • dongzhong5756 2017-01-17 04:08
    关注

    The regex didn't work because after the S[UID] you expect 2 | as per the regex but in the first input string there is only one.

    One fix is to make the second group optional and move out the string end anchor $

    (N(.*?)S([UID])\|(.*?))(?:B|\|.?)?$
    

    Regex Demo


    Or may be more simpler as
    N.*?S[UID]\|.*$
    

    Regex Demo

    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示