Search code examples
phpregexpcre

Regex capture group 2 to capture what does not match capture group 1


I am wanting to capture Owner or Builder in group 1, otherwise any text up until hyphen and/or space followed by a digit is encountered in group 2, then any digit til end of line in group 3 from the following text:-

PRD Hervey Bay 07 2525 1919
RG Sprake & Co Maryborough 4141 2424
Owner Robert - 0429 966 391
Owner Maureen - 07 8888 4444 - 0422000 111
Owner Wayne - 0430 555 666
Builder Scott - 0444 555 777

The expression I am currently using:-

/(Owner|Builder)([^-]+)-|\s+(\d.*)/gm

Does not match 'PRD Hervey Bay' and 'RG Sprake & Co Maryborough' in group 2.

Any help appreciated!

Trent.


Solution

  • You can use this regex to get the results you want:

    /^(Owner |Builder )?(.*?)(?=-| \d)[- ]+(.*)$/m
    

    It looks for (optionally) the string Owner or Builder; followed by some minimal number of characters until a - or a space followed by a digit; followed by some number of - or space, then characters until end of line. To use in PHP:

    $text = <<<EOD
    PRD Hervey Bay 07 2525 1919
    RG Sprake &amp; Co Maryborough 4141 2424
    Owner Robert - 0429 966 391
    Owner Maureen - 07 8888 4444 - 0422000 111
    Owner Wayne - 0430 555 666
    Builder Scott - 0444 555 777
    EOD;
    preg_match_all('/^(Owner |Builder )?(.*?)(?=-| \d)[- ]+(.*)$/m', $text, $matches);
    print_r($matches);
    

    Output:

    Array ( 
      [0] => Array (
        [0] => PRD Hervey Bay 07 2525 1919
        [1] => RG Sprake &amp; Co Maryborough 4141 2424
        [2] => Owner Robert - 0429 966 391
        [3] => Owner Maureen - 07 8888 4444 - 0422000 111
        [4] => Owner Wayne - 0430 555 666
        [5] => Builder Scott - 0444 555 777 
        )
      [1] => Array (
        [0] => 
        [1] =>
        [2] => Owner
        [3] => Owner
        [4] => Owner
        [5] => Builder
        )
      [2] => Array (
        [0] => PRD Hervey Bay
        [1] => RG Sprake &amp; Co Maryborough
        [2] => Robert
        [3] => Maureen
        [4] => Wayne
        [5] => Scott
        )
      [3] => Array (
        [0] => 07 2525 1919
        [1] => 4141 2424
        [2] => 0429 966 391
        [3] => 07 8888 4444 - 0422000 111
        [4] => 0430 555 666
        [5] => 0444 555 777
        )
      )
    

    Demo on 3v4l.org

    Note that this does leave some spaces and - in the last group, you can most easily remove them (if desired) by using array_walk:

    array_walk($matches[3], function (&$v) { $v = preg_replace('/[^\d]/', '', $v); });
    

    Demo on 3v4l.org