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?
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}