I have something like this
a:2:{s:4:"Test";s:29:"asdf'a' ;"serialize here;?"!";s:5:"test2";a:1:{s:4:"Test";s:15:"serialize here!";}}
and I want to make a regex in order to match both examples below:
s:15:"serialize
s:29:"asdf'a' ;"serialize
How can I do that?
I tried something like s:d+:".+?serialize
But it doesn’t work.
2
Answers
You can use the following.
Granted, there must be no colon, :, between the s:d: and "serialize".
Matches
You can use:
A couple notes regarding you attempt:
s:d+
would also match something like "likes:123". To avoid this I addedb
,.+?
matches at least one symbol, and in your example one of the expected outputs has zero. Also, lazy modifier works only for the right side of matching: this is the reason it matched form the firsts:d+
till theserialize
and not nearest pair.To match closes pair of
s:d+
andserialize
I’m using(?:(?!bs:d+).)*?
. It matches smallest possible block, that doesn’t contains:d+
itself.Demo here.