Search code examples
phpregexpreg-matchpreg-match-all

Get all pattern Word fom a Text


If I have a text, how can I get all the strings with a certain pattern from the text? For example I want all the strings with this patters:

The word CER followed by any number followed by one of this characters ;, ,, . or space .

For exemple from the text bellow I wold like to have in an array the following results : CER123 , CER23 , CER01 , CER24

Lorem CER123 ipsum dolor sit amet, quod copiosae CER23,CER01;CER24 insolens et usu, vis CER34ERD ut saperet civibus accommodare.

I've tried this:

preg_match_all("/CER[0-9*]?/",$content,$m);

but it returns : CER1 , CER2 , CER0 , CER2 and CER3


Solution

  • Use +, add a capturing group and create a character class for the characters you require after the value you need to get:

    preg_match_all('/(CER[0-9]+)[;,.\s]/',$content,$m);
                     ^        ^^^^^^^^^
    

    See the regex demo

    Pattern explanation:

    • (CER[0-9]+) - Group 1 capturing the parts of text you need:
      • CER - a sequence of literal characters CER (NOTE: If you need whole word CER only, after a non-word char or at the start of the string, add word boundaries: \bCER\b)
      • [0-9]+ (=\d+) - 1 or more digits
    • [;,.\s] - any char inside the character class: ;, ,, . or \s (whitespace - replace with a space if you only mean a regular space).

    PHP demo:

    $re= '/(CER[0-9]+)[;,.\s]/';
    $content = "Lorem CER123 ipsum dolor sit amet, quod copiosae CER23,CER01;CER24 insolens et usu, vis CER34ERD ut saperet civibus accommodare."; 
    preg_match_all($re, $content, $m);
    print_r($m[1]);