Trying to store interface when preg_match_all is executed using php

Your RegEx is incorrect, it should be:

STRING: \"?(.*?)\"? $
STRING: \"?              // Match the string 'STRING: ' followed by an optional double quote
           (.*?)         // Non-greedy capture anything until the next character in the pattern
                \"? $    // Match an optional double quote followed by a space and the end of the string {$}

Example

preg_match_all("/STRING: \"?(.*?)\"? $/", $str, $matches);
print_r($matches);

/* Output:
    Array
    (
        [0] => Array
            (
                [0] => STRING: "interface900" 
            )
    
        [1] => Array
            (
                [0] => interface900
            )
    
    )
*/

preg_match("/STRING: \"?(.*?)\"? $/", $str, $matches);
print_r($matches);

/* Output:
    Array
    (
        [0] => STRING: "interface900" 
        [1] => interface900
    )
*/

Why your RegEx failed

^STRING: \"?(.*?)\"?$
^                        // Matches the start of the string {DOESN'T MATCH}
 STRING: \"?             // Match the string 'STRING: ' followed by an optional double quote
            (.*?)        // Non-greedy capture anything until the next character in the pattern
                 \"?$    // Match an optional double quote followed immediately by the end of the string {DOESN'T MATCH}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top