448191 Posted December 12, 2008 Share Posted December 12, 2008 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;} Quote Link to comment https://forums.phpfreaks.com/topic/136752-getting-backreferences-to-all-matches-of-a-caturing-group-instead-of-the-last/ Share on other sites More sharing options...
nrg_alpha Posted December 12, 2008 Share Posted December 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/136752-getting-backreferences-to-all-matches-of-a-caturing-group-instead-of-the-last/#findComment-714298 Share on other sites More sharing options...
.josh Posted December 13, 2008 Share Posted December 13, 2008 are you wanting to capture any of that other stuff, or just those 3 things? Quote Link to comment https://forums.phpfreaks.com/topic/136752-getting-backreferences-to-all-matches-of-a-caturing-group-instead-of-the-last/#findComment-714322 Share on other sites More sharing options...
448191 Posted December 13, 2008 Author Share Posted December 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/136752-getting-backreferences-to-all-matches-of-a-caturing-group-instead-of-the-last/#findComment-714338 Share on other sites More sharing options...
.josh Posted December 13, 2008 Share Posted December 13, 2008 $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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/136752-getting-backreferences-to-all-matches-of-a-caturing-group-instead-of-the-last/#findComment-714386 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.