I'm trying to get the values ad.test.com
and 768
from the string "adress=ad.test.com
port=768"
with this regex but it doesn't work :/
$res = preg_split("/^adresse=([a-z]).*port=([0-9]{3})/", $test) ;
I'm trying to get the values ad.test.com
and 768
from the string "adress=ad.test.com
port=768"
with this regex but it doesn't work :/
$res = preg_split("/^adresse=([a-z]).*port=([0-9]{3})/", $test) ;
This should capture the 2 data points you want:
^adress=([a-z.]+).*port=([0-9]{3})
You also want to use this with preg_match
, not preg_split
.
preg_match('/^adress=([a-z.]+).*port=([0-9]{3})/s', 'adress=ad.test.com
port=768', $match);
echo $match[1] . ' ' . $match[2];
Demo: https://3v4l.org/Ih929
Notes on why your regex failed:
.
, and the character class wasn't quantified.adress
is in your string but you are looking for addresse
in your regex.
doesn't match new lines unless the s
modifier is used.Regex demo: https://regex101.com/r/3MSiDh/1/
A more accurate regex that won't require s
modifier:
^adress=([a-z.]+?)\s*port=(\d{3})
Demo 2: https://regex101.com/r/3MSiDh/3/