doubei2340 2019-06-16 16:26
浏览 93

返回多个匹配但仅使用PHP和RegEX直到第一次出现模式

I am having a dataset that looks like

I(0,123...789){
A(0,567...999){.......n=Marc.....}
B(2,655...265){..................}
C(3,993...333){..................}
M(8,635...254){.................;}
}
O(0,345...789){
A(0,567...999){.......n=Marc.....}
B(2,876...775){..................}
C(3,993...549){..................}
M(8,354...987){.................;}
}
I(0,987...764){
A(0,567...999){.......n=Marc.....}
B(2,543...265){..................}
C(7,998...933){..................}
M(8,645...284){.................;}
}
B(0,123...789){
.......
}
I(0,987...764){
A(0,567...999){.......n=John.....}
B(2,543...265){..................}
C(7,998...933){..................}
M(8,645...284){.................;}
}

I am trying to return all I "sections" so starting from "I" until the closing tag that comes after the ;} but only if the "I" section contains n=Marc.

So far I came with

^([I]\(.*\){.*n=Marc.*^[M]\(.*;}.)}

https://regex101.com/r/VSuZh5/1

However in some cases, when data has a pattern like

I(0,123...789){
A(0,567...999){.......n=Marc.....}
B(2,655...265){..................}
C(3,993...333){..................}
M(8,635...254){.................;}
}
O(0,345...789){
A(0,567...999){.......n=Marc.....}
B(2,876...775){..................}
C(3,993...549){..................}
M(8,354...987){.................;}
}

The regular expression returns both the I and O section. Is there a way to make sure it always return the I section?

  • apologies for the dataset, it's huge and contains a lot of sensitive data which I can't make public.*
  • 写回答

3条回答 默认 最新

  • dqp10099 2019-06-16 16:35
    关注

    My guess is that we want an expression to return the O section that has n=Marc in it, something similar to:

    (?=O\()([\s\S]*?n=Marc[\s\S]*?;}\s*})
    

    Or maybe:

    (?=O\()([\s\S]*?n=Marc[\s\S]*?;})\s*}
    

    Demo 1

    For I sections we'd simply change O to I:

    (?=I\()([\s\S]*?n=Marc[\s\S]*?;})\s*}
    

    Demo 2

    Test

    $re = '/(?=I\()([\s\S]*?n=Marc[\s\S]*?;})\s*}/m';
    $str = 'I(0,123...789){
    A(0,567...999){.......n=Marc.....}
    B(2,655...265){..................}
    C(3,993...333){..................}
    M(8,635...254){.................;}
    }
    O(0,345...789){
    A(0,567...999){.......n=Marc.....}
    B(2,876...775){..................}
    C(3,993...549){..................}
    M(8,354...987){.................;}
    }
    I(0,987...764){
    A(0,567...999){.......n=Marc.....}
    B(2,543...265){..................}
    C(7,998...933){..................}
    M(8,645...284){.................;}
    }
    B(0,123...789){
    .......
    }
    I(0,987...764){
    A(0,567...999){.......n=John.....}
    B(2,543...265){..................}
    C(7,998...933){..................}
    M(8,645...284){.................;}
    }';
    
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    
    foreach ($matches as $key => $I) {
        echo $I[0] . "
    ";
    }
    

    Output

    I(0,123...789){
    A(0,567...999){.......n=Marc.....}
    B(2,655...265){..................}
    C(3,993...333){..................}
    M(8,635...254){.................;}
    }
    I(0,987...764){
    A(0,567...999){.......n=Marc.....}
    B(2,543...265){..................}
    C(7,998...933){..................}
    M(8,645...284){.................;}
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现