Jump to content

Getting backreferences to all matches of a caturing group instead of the last


448191

Recommended Posts

I have this pattern (matching a PHP serialized string):

 

(\w:\d*?:"([a-zA-Z0-9_]*?)":\d*:){(?:((\w:\d*?:"##0[\w\*]*?##0([a-zA-Z0-9_]*?)";([a-z])):(.*?);)*)}

 

 

Problem is I need to capture all occurrences (keys, datatypes and value) of the underlined groups. Right now every match overwrites the backreferences of the previous match, but wasn't there a way to avoid that? I'm having some sort of deja vu feeling about the thing.

 

Example string:

 

O:16:"DomainObjectStub":4:{s:21:"##0DomainObjectStub##0_id";i:1;s:14:"##0*##0_someString";s:9:"someValue";s:28:"##0DomainObjectStub##0_someFloat":0.1000000000000000055511151231257827021181583404541015625;s:12:"##0*##0_someBool";b:0;}

Link to comment
Share on other sites

Wow 448191 (err.. nice screen name ;) ).. Got to be honest, that sample string turns my head and stomach a little bit.

Could you provide the same sample string with the sections within it highlighted red that you wish the regex pattern to match / capture?

 

Would make it easier for the rest of us to see what it is exactly in the string you need, as opposed to trying to decypher your current regex pattern.

Link to comment
Share on other sites

Wow 448191 (err.. nice screen name ;) ).. Got to be honest, that sample string turns my head and stomach a little bit.

Could you provide the same sample string with the sections within it highlighted red that you wish the regex pattern to match / capture?

 

Would make it easier for the rest of us to see what it is exactly in the string you need, as opposed to trying to decypher your current regex pattern.

 

Actually this is only a starting point. This won't work with nested compound types, so it would get (a lot) more complicated. Not to be rude, but you don't see what that says it won't help me to explain it to you.

 

are you wanting to capture any of that other stuff, or just those 3 things?

 

Those three things and the second group (matching the first DomainObjectStub in the example). I should've removed the first group.

Link to comment
Share on other sites

$string = 'O:16:"DomainObjectStub":4:{s:21:"##0DomainObjectStub##0_id";i:1;s:14:"##0*##0_someString";s:9:"someValue";s:28:"##0DomainObjectStub##0_someFloat":0.1000000000000000055511151231257827021181583404541015625;s:12:"##0*##0_someBool";b:0;}';
preg_match_all('/(?:.+?:.+?:"(\w+)":.+?:{)?(?:\w+:\w+:".*?(_\w+)";(.+?).+?);)/',$string,$matches);
echo "<pre>";
print_r($matches);
echo "</pre>";

 

Array (
   [0] => Array (
      [0] => O:16:"DomainObjectStub":4:{s:21:"##0DomainObjectStub##0_id";i:1;
      [1] => s:14:"##0*##0_someString";s:9:"someValue";
      [2] => s:28:"##0DomainObjectStub##0_someFloat":0.1000000000000000055511151231257827021181583404541015625;
      [3] => s:12:"##0*##0_someBool";b:0;
   )
   [1] => Array (
      [0] => DomainObjectStub
      [1] => 
      [2] => 
      [3] => 
   )
   [2] => Array (
      [0] => _id
      [1] => _someString
      [2] => _someFloat
      [3] => _someBool
   )
   [3] => Array (
      [0] => i
      [1] => s
      [2] => d
      [3] => b
   )
   [4] => Array (
      [0] => 1
      [1] => 9:"someValue"
      [2] => 0.1000000000000000055511151231257827021181583404541015625
      [3] => 0
   )
)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.