Regex to capture everything before first optional string

前端 未结 5 707
旧时难觅i
旧时难觅i 2020-12-16 02:20

I want to capture a pattern upto but not including the first instance of an optional other pattern with preg_match, eg:

ABCDEFGwTW$%                                  


        
相关标签:
5条回答
  • 2020-12-16 02:58

    Another way to do it:

    $str = 'Q$TQ@#%GEFGw35hqb';
    $res = preg_split('/EFG/', $str);
    print_r($res);
    
    0 讨论(0)
  • 2020-12-16 03:01

    You can have the result with a lot less confusion:

    Just check a simpler version of the pattern to match, and if not, use the original string:

    <?php
    $match = 'Q$TQ@#%GEFGw35hqb';
    if (preg_match('/^(.*)EFG/', $match, $matches)) {
        $match = $matches[1];
    }
    
    echo $match;
    
    0 讨论(0)
  • 2020-12-16 03:02

    Try this: (.*?)(?:EFG|$)

    This will match any character (as few as possible) until it finds EFG.

    0 讨论(0)
  • 2020-12-16 03:08

    You can use

    '/(.*?)(?=EFG|$)/'
    
    0 讨论(0)
  • 2020-12-16 03:13

    Using preg_match() with a pattern that uses lazy matching and a lookahead is going to take more steps than just using preg_replace() with greedy matching (and no lookarounds) and simply replacing the optional match with an empty string. If the needle doesn't exist, then nothing is changed in the string. Super easy.

    Code: (Demo)

    $strings = [
        'ABCDEFGwTW$%',
        '@Q%HG@H%hg afdgwsa g   weg#D DEFG',
        '@Q%HDEFG@H%hg afdgwsa g   weg#D DEFG',
        'No needle in the haystack',
    ];
    
    var_export(preg_replace('/EFG.*/', '', $strings));
    

    Output:

    array (
      0 => 'ABCD',
      1 => '@Q%HG@H%hg afdgwsa g   weg#D D',
      2 => '@Q%HD',
      3 => 'No needle in the haystack',
    )
    
    0 讨论(0)
提交回复
热议问题