Search code examples
phpregexpreg-match-all

Regex in php excluding values


I have these lines of text ($text variable)

CC 1,00
SS 1,00
PP 1,00
1,00
FF 1,00

now I would like to have only the numeric part of each line returned to me. But the important thing must be that I don't have to take lines that start with certain characters, like I would like to exclude "SS", "PP" and "FF". So I would like it to return me only the values of "CC" and only the numeric ones without leading characters.

In the meantime I have done this but it doesn't seem to work

preg_match_all("/((?!.SS|.FF|.PP).*\d{1,2}[\,\.]{1}\d{1,2})\w+/", $text, $matches);

Unfortunately, I don't get the desired result, where am I wrong? Thanks


Solution

  • I modified your regex to this: ^(?!SS|FF|PP).*(\d{1,2}[,.]\d{1,2})

    Test here: https://regex101.com/r/SAivli/2

    $re = '/^(?!SS|FF|PP).*(\d{1,2}[,.]\d{1,2})$/m';
    $str = 'CC 1,00
    SS 1,00
    PP 1,00
    1,00
    FF 1,00';
    
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    
    // Print the entire match result
    print_r($matches);
    //Array ( [0] => Array ( 
    //                        [0] => CC 1,00 
    //                        [1] => 1,00 
    //                      )
    //         [1] => Array ( [0] => 1,00
    //                        [1] => 1,00 
    //                      )
    //        ) 
    

    This matches both the numbers that:

    • begin with CC
    • dont have any leading alpha characters