Reputation: 961
I have a string mystring
that contains newlines in it :
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::host]] {
"sub1.sub.dom" {
pool /networkname/hostname_80
}
"sub2.sub.dom" {
pool /anothernetworkname/anotherhostname_80
}
default {
drop
}
}
}
I'm trying to extract the pools from that string using preg_match_all
but I cant get the wanted result...I'm currently using :
preg_match_all('/\/([^"]+)$/m',$mystring,$result);
the result is :
Array
(
[0] => networkname/hostname_80
}
[1] => anothernetworkname/anotherhostname_80
}
default {
drop
}
}
})
I have two questions :
thanks.
Upvotes: 1
Views: 516
Reputation: 627083
It seems you just want to extract chunks of non-whitespace chars after a word pool
, 1+ whitespaces and /
.
You may use
'~\bpool\h+/\K\S+$~m'
with preg_match_all
function in PHP. See the regex demo.
Details
\bpool
- a whole word pool
(the next char should be a horizontal whitespace and \b
is a word boundary)\h+
- 1+ horizontal whitespace chars/
- a /
char\K
- match reset operator to discard the text matched so far\S+
- 1+ chars other than whitespace$
- end of line (due to m
modifier).$re = '~\bpool\h+\K/\S+$~m';
$str = "when HTTP_REQUEST {\nswitch -glob [string tolower [HTTP::host]] {\n \"sub1.sub.dom\" {\n pool /networkname/hostname_80\n }\n \"sub2.sub.dom\" {\n pool /anothernetworkname/anotherhostname_80\n }\n default {\n drop \n }\n}\n}";
if (preg_match_all($re, $str, $matches)) {
print_r($matches[0]);
}
Output:
Array
(
[0] => networkname/hostname_80
[1] => anothernetworkname/anotherhostname_80
)
Upvotes: 1
Reputation: 4116
Wiktor regex is right but it missing .
after space \S
, see preg_match_all Demo
/\bpool\h+\K\S.+$/m
Regex Explanation:
Global pattern flags
Upvotes: 2