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 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)