1

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

0

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

You can anchor the negative lookahead to the start of the string, and omit the leading dots in .SS as it matches a single character.

You can also omit the {1} and the backslashes in [\,\.]

As there are no other characters in the examples besides spaces and 2 uppercase characters, you can change .* to match [A-Z]{2} followed by optional horizontal whitespace chars \h*

Omit the \w+ at the end because there are no more word characters and the + expects at least a single char.

^(?!SS|FF|PP)[A-Z]{2}\h*\K\d{1,2}[,.]\d{1,2}$
  • ^ Start of string
  • (?!SS|FF|PP) Assert not any of the alternatives to the right
  • [A-Z]{2} Match 2 uppercase chars
  • \h*\K Match optional whitespace chars and forget what is matched so far
  • \d{1,2}[,.]\d{1,2} Match 1-2 digits followed by either . or , and again 1-2 digits
  • $ End of string

regex demo

$re = '/^(?!SS|FF|PP)[A-Z]{2}\h*\K\d{1,2}[,.]\d{1,2}$/m';
$text = 'CC 1,00
SS 1,00
PP 1,00
1,00
FF 1,00';

preg_match_all($re, $text, $matches);

print_r($matches[0]);

Output

Array
(
    [0] => 1,00
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.