skip to Main Content

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


  1. You can use the following.

    Granted, there must be no colon, :, between the s:d: and "serialize".

    s:d+:[^:]+serialize
    

    Matches

    s:29:"asdf'a' ;"serialize
    s:15:"serialize
    
    Login or Signup to reply.
  2. You can use:

    bs:d+:"(?:(?!bs:d+).)*?serialize
    

    A couple notes regarding you attempt:

    • s:d+ would also match something like "likes:123". To avoid this I added b,
    • .+? 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 first s:d+ till the serialize and not nearest pair.

    To match closes pair of s:d+ and serialize I’m using (?:(?!bs:d+).)*?. It matches smallest possible block, that doesn’t contain s:d+ itself.

    Demo here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search