Results 1 to 5 of 5

Thread: [RESOLVED] PHP preg_match NOT equal to backreference

  1. #1

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    Resolved [RESOLVED] PHP preg_match NOT equal to backreference

    Is there a way to negate backreferences in a PHP preg_match test?

    Code:
       .
       .
       .
    1000| asdf34asdfasdf
    1000| asdfadf43tasda34tfasdff
    1000| qewrqwerqwt4er
    1001| weklrjwelkr3jlkw
    1001| welrjewrek455lwjrlkw
    1001| trewewerwehklrjwelkrjlkw
    1001| welrjewrek4hlwjrlkw
    1001| fgfweklrjh4welkrjlkw
    1001| welrjewh4reklwjrlkw
    1002| weroiw5eewkjekwjw
    1002| weroiweewkjekwjw
    1002| we234roiweewkjekwjw
    1002| weroiwe5ewkjekwjw
    1002| weroiw324eewkjekwjw
       .
       .
       .
    I want to be able to grab the data from the first instance of 1000 until the first instance of 1001, but using preg_match_all, continue to perform this for 1001, 1002, etc.

    effectively producing an array containing:

    Code:
    [1]
    1000| asdf34asdfasdf
    1000| asdfadf43tasda34tfasdff
    1000| qewrqwerqwt4er
    
    [2]
    1001| weklrjwelkr3jlkw
    1001| welrjewrek455lwjrlkw
    1001| trewewerwehklrjwelkrjlkw
    1001| welrjewrek4hlwjrlkw
    1001| fgfweklrjh4welkrjlkw
    1001| welrjewh4reklwjrlkw
    
    [3]
    1002| weroiw5eewkjekwjw
    1002| weroiweewkjekwjw
    1002| we234roiweewkjekwjw
    1002| weroiwe5ewkjekwjw
    1002| weroiw324eewkjekwjw
    Any ideas?
    IWS

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: PHP preg_match NOT equal to backreference

    and create this array with just a call to preg_match_all()? no. you'll need to process the array somehow:

    PHP Code:
    <?php
    $str 
    = <<<EOF
    1000| asdf34asdfasdf
    1000| asdfadf43tasda34tfasdff
    1000| qewrqwerqwt4er
    1001| weklrjwelkr3jlkw
    1001| welrjewrek455lwjrlkw
    1001| trewewerwehklrjwelkrjlkw
    1001| welrjewrek4hlwjrlkw
    1001| fgfweklrjh4welkrjlkw
    1001| welrjewh4reklwjrlkw
    1002| weroiw5eewkjekwjw
    1002| weroiweewkjekwjw
    1002| we234roiweewkjekwjw
    1002| weroiwe5ewkjekwjw
    1002| weroiw324eewkjekwjw
    EOF;
      
      
    $pattern '/(\d{4})\| [a-z]+/i';
      
      
    preg_match_all($pattern$str$matches);
      
      
    // array and counter
      
    $parsed = array();
      
    $j = -1;
      
    $n 0;
      
      
    // loop through
      
    for($i 0$i count($matches[1]); $i++){
        
        
    $num   $matches[1][$i];
        
    $match $matches[0][$i];
        
        
    // if the number has changed:
        
    if($num != $n){
          
          
    // increase counter
          
    $j++;
          
          
    // keep a reference of the number we're on
          
    $n $num;
          
          
    // "set" the initial state of this key
          
    $parsed[$j] = "";
        }
        
        
    // store entire match
        
    $parsed[$j] .= $match "\n";
      
      }
      
      
    print_r($parsed);
      
    ?>
    which produces:

    Code:
    Array
    (
        [0] =>
    1000| asdf
    1000| asdfadf
    1000| qewrqwerqwt
    
        [1] =>
    1001| weklrjwelkr
    1001| welrjewrek
    1001| trewewerwehklrjwelkrjlkw
    1001| welrjewrek
    1001| fgfweklrjh
    1001| welrjewh
    
        [2] =>
    1002| weroiw
    1002| weroiweewkjekwjw
    1002| we
    1002| weroiwe
    1002| weroiw
    
    )

  3. #3

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    Re: PHP preg_match NOT equal to backreference

    Thanks kows (I should probably preface every thread with that )

    So have you inadvertently answered the question of:

    Is there a way to negate backreferences in a PHP preg_match test?
    I was hoping that one could perform:
    Code:
    (?<id>[0-9]+?)\|(?<data>.*?)([^(P?<id>)])
    Or something to that effect...
    IWS

  4. #4

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    Re: PHP preg_match NOT equal to backreference

    The BIG problem is... I'm working with files in excess of 70Mb...

    The average number of lines per file is in excess of several hundred thousand.

    I don't want the array index to overflow on me...
    IWS

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: PHP preg_match NOT equal to backreference

    you should give it a try and see if you do have an overflow then. you shouldn't, though. if you do, however, then you would have the same overflow if one regular expression did do it all anyway. the array would be the same size. if PHP is going to crash by you doing it manually, it's going to crash when it creates the array itself, too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width