DarkWater Posted August 29, 2008 Share Posted August 29, 2008 Hey everyone. Well, I'm pretty proficient in regular expressions, and I stumbled upon a little problem that I really couldn't find a solution to (but then again, I didn't search too hard, hoping someone here would know. ) Here we go: Let's say I had this regex: /^(?<!\$)(?P<word>\w+)$/ It tests to make sure that a string doesn't start with a $ and contains only word characters. Simple enough, right? Well, I was just playing around with named capture groups to see how PHP handles them. It seems that they're added as an associative array key to the matches array: <?php $str = 'testing'; $matches = array(); echo preg_match('/^(?<!\$)(?P<word>\w+)$/', $str, $matches); echo "\n"; print_r($matches); ?> Outputs: Array ( [0] => testing [word] => testing [1] => testing ) Now, how would you reference that named capture in preg_replace() without using the numbered reference? I know that you can use it inside the regex for backreferencing with (?P=word). >_> Anyone want to throw some advice out there? xD Quote Link to comment https://forums.phpfreaks.com/topic/121811-referencing-named-groups-in-preg_replace/ Share on other sites More sharing options...
effigy Posted August 29, 2008 Share Posted August 29, 2008 I cannot find any information on this. My assumption is that it doesn't exist because there isn't a need for it. A data structure is being built when matching, so it's nice to be able to customize the key, whereas replacing is solely done by the engine. In the event that your replaces get nasty, the best approach is to use the /x modifier: / ### 1. Name (...) ### 2. Address (...) ### 3. And so forth... (...) /x Quote Link to comment https://forums.phpfreaks.com/topic/121811-referencing-named-groups-in-preg_replace/#findComment-628746 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Author Share Posted August 29, 2008 Yeah, I've used free spacing before for comments and stuff. Thanks though. Although, I think I read somewhere that in 5.2.4 you can use: \k<name>, \k'name', \k{name} or \g{name} To reference your named capture groups. Not sure if it would work in preg_replace() though. I'll need to go test it later. >_< Found it on this page Quote Link to comment https://forums.phpfreaks.com/topic/121811-referencing-named-groups-in-preg_replace/#findComment-628768 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Author Share Posted August 29, 2008 Okay, apparently those don't work in preg_replace() either, but I do see where you're coming from, effigy. Guess that won't work, lol. But it IS handy to have in preg_match(). Btw, if someone does know how to do this, please say so. And to avoid any confusion, I'll say before hand that I changed the regex to: /\b(?<!\$)(?P<word>\w+)\b/ Lets me get any words that don't have a $ at the start from a string. Just to see how preg_match_all() handled named groups. Same as preg_match_all() always handles things; just a multi-dimensional array with the capture group's name included as one of the keys, which is pretty good to know. Quote Link to comment https://forums.phpfreaks.com/topic/121811-referencing-named-groups-in-preg_replace/#findComment-628781 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Author Share Posted August 29, 2008 Alright, I did a little bit more searching and still came up with nothing. I still think it would be cool with replacing too, but at least PHP handles it nicely with the assoc. array. Quote Link to comment https://forums.phpfreaks.com/topic/121811-referencing-named-groups-in-preg_replace/#findComment-628964 Share on other sites More sharing options...
nrg_alpha Posted August 29, 2008 Share Posted August 29, 2008 Well, I looked it up in the mastering regular expressions book.. it seems that in every instance of named captures, it always involves preg_match only. So perhaps it is only usable with this function? I could not find anything in the php.net manual (unless I'm just searching for the incorrect terms). :: Quote Link to comment https://forums.phpfreaks.com/topic/121811-referencing-named-groups-in-preg_replace/#findComment-628989 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Author Share Posted August 29, 2008 Yeah, and I mean, you can still use it for backreferencing at least in preg_match(). Quote Link to comment https://forums.phpfreaks.com/topic/121811-referencing-named-groups-in-preg_replace/#findComment-629020 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.