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