2

I would like to store the interface name only. So in this case, I would like to store Interface900. Below is my code. Can't seem to figure out how to do this.

$str = '[TIMETRA-VRTR-MIB::vRtrIfName.22.178] => STRING: "interface900" ';

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

Also tried

preg_match_all('(STRING: ")([\w-_]*)', $str, $matches);
print_r($matches);

IN both cases, it's not printing Interface900. I'm not too good with regex or php. Can someone help me please?

Steven
  • 6,053
  • 2
  • 16
  • 28
Mike T
  • 47
  • 3

2 Answers2

0

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}
Steven
  • 6,053
  • 2
  • 16
  • 28
0

The second pattern has the value in capturing group 2. Note that \w also matches an underscore.

A bit more precise pattern without a capturing group could be asserting the closing " and repeat the character class at least 1+ more times to prevent an empty match.

\bSTRING:\h+"\K[\w-]+(?=")

Explanation

  • \bSTRING: Match STRING:
  • \h+" Match 1+ horizontal whitespace chars and "
  • \K[\w-]+ Reset the match buffer, then match 1+ times either a word character or -
  • (?=") Positive lookahead, assert " directly to the right

Regex demo | Php demo

$re = '/\bSTRING:\h+"\K[\w-]+(?=")/';
$str = '[TIMETRA-VRTR-MIB::vRtrIfName.22.178] => STRING: "interface900" ';

preg_match_all($re, $str, $matches);
print_r ($matches[0]);

Output

Array
(
    [0] => interface900
)

Matching the whole string, you might also take the leading square brackets into account, and match 1+ word chars followed by optionally repeating a - and 1+ word chars to prevent matching only hyphens.

You might also use the capturing group for example:

\[[^][]+\]\h+=>\h+STRING:\h+"(\w+(?:-\w+)*)"

Regex demo | php demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70