I am trying to match a a part of a string with delimiters. The strings I would like to retrive are currency symbols.
Strings look like:
0.040-aa-¥-bb-234,5.4
234,5.4-aa-CN¥-bb-blahblahb
-aa-$-bb-sdfsefblah3949
34blah-aa-$-bb-
and so o开发者_高级运维n.
This should be extracted into:
¥
CN¥
$
$
So essentially, I will have a string made up of any amount of characters, with currency symbols. The currency symbol will always have -aa- at the front and -bb- at the back. I would like to retrive what's between those 2 delimiters.
Currently, I have came up with this expression: -aa-[^\]]+-bb-
, however, it includes the -aa- and -bb-. How do I exclude those parts?
Cheers :)
preg_match('/(?<=-aa-)[^-]*(?=-bb-)/', $string, $matches)
gives you the currency symbol as the entire match result.
(?<=...)
and (?=...)
are lookaround assertions, meaning they check for the presence of the enclosed regexes before or after the current position without making them a part of the overall match.
To retrieve the specific part of the expression, use a capturing group, eg
if (preg_match('/-aa-(.+?)-bb-/', $string, $matches) == 1) {
$symbol = $matches[1];
}
精彩评论