Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/121878-question-regarding-named-captures/
Share on other sites

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 ;)

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.

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.