nrg_alpha Posted August 29, 2008 Share Posted August 29, 2008 Apparently , the format for naming captures is (?P<capture_name_here>).. but I have accidently entered (?<capture_name_here>) and this still seems to work. Am I misunderstanding the 'P' aspect of this? For example: $str = 'This is number 123.'; preg_match('#(?<numbers>\b\d+\b)#', $str, $match); echo $match["numbers"]; ouputs: 123 Quote Link to comment https://forums.phpfreaks.com/topic/121878-question-regarding-named-captures/ Share on other sites More sharing options...
effigy Posted August 29, 2008 Share Posted August 29, 2008 This does not work for me. What version of PHP are you running? Quote Link to comment https://forums.phpfreaks.com/topic/121878-question-regarding-named-captures/#findComment-628838 Share on other sites More sharing options...
nrg_alpha Posted August 29, 2008 Author Share Posted August 29, 2008 This does not work for me. What version of PHP are you running? I am using 5.2.6 Quote Link to comment https://forums.phpfreaks.com/topic/121878-question-regarding-named-captures/#findComment-628854 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 Lol, I knew someone else had to have read my topic. Anyway, that's so weird. It does work for me (I'm on 5.2.6), but I know it shouldn't. Quote Link to comment https://forums.phpfreaks.com/topic/121878-question-regarding-named-captures/#findComment-628881 Share on other sites More sharing options...
nrg_alpha Posted August 29, 2008 Author Share Posted August 29, 2008 Lol, I knew someone else had to have read my topic. Anyway, that's so weird. It does work for me (I'm on 5.2.6), but I know it shouldn't. Funny you mention someone reading your topic.. it was indeed me.. I was orginially going to post this in your thread.. (but this in reality is bad forum ettiquette).. I didn't want to 'hijack' your thread, so I started this one instead (out of respect and to keep these topics (while simular in dealing with named captures- are different) separate). The reason I brougt this up was in part your thread, that in turn reminded me of chapter 3 in Mastering regular expressions (which has a page that discusses named captures). When I was following along in the book, I mistyped the capture without the 'P'. So Upon reading your thread, I figured, what the heck.. might as well come forward with this and see what's up Quote Link to comment https://forums.phpfreaks.com/topic/121878-question-regarding-named-captures/#findComment-628909 Share on other sites More sharing options...
nrg_alpha Posted August 29, 2008 Author Share Posted August 29, 2008 Ok, so I emailed the book's author, who was kind enough to send me a link to the PCRE page: http://www.pcre.org/pcre.txt I did a search / find for 'named', and sifted through the text document. 'In PCRE, a subpattern can be named in one of three ways: (?<name>...) or (?'name'...) as in Perl, or (?P<name>...) as in Python.' According to PCRE homepage, they are up to version 7.7 (which is reflected in the version of use starting from PHP 4.4.9 according the PHP's homepage). I am wondering if it is possible that some PCRE option has been changed at complile time to be released in a later version of PHP that enables it to support the '(?<name>...)' format. In either case, this is not such a large deal, as we can always play it safe by sticking with the '(?P<name>...)' format. At least this way, it seems like a safe bet that your local machine / webhosting provider's version of PHP will execute without issues. I tried '(?<name>...)' on my live site (PHP version 5.2.5) and it still worked. Quote Link to comment https://forums.phpfreaks.com/topic/121878-question-regarding-named-captures/#findComment-629141 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 I strongly doubt that they'd change PCRE's internals just for one PHP option. If that's how named captures are done in Perl (never used them in Perl yet, just other regex things. >_<), then they won't remove it. Nice find. Quote Link to comment https://forums.phpfreaks.com/topic/121878-question-regarding-named-captures/#findComment-629156 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.