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 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口