Page 1 of 1

Naming Back References

Posted: Fri Jun 17, 2005 3:48 pm
by nickvd
In searching for form validation patterns, I came across the following phone number pattern that is VERY tolerant (allows, brackets, hyphens, x|X|Ext for extensions,e tc..)

Code: Select all

$pat = '#^(?:(?P<1>ї(])?(?P<AreaCode>ї2-9]\d{2})(?(1)ї)]ї ]?|ї- \/.]?))?(?P<Prefix>ї1-9]\d{2})ї- .]?(?P<Suffix>\d{4})(?:(?:ї ]+|\s*?x\s*?|\s*?ext\s*?\.?ї ]?){1,2}(?P<Ext>\d{1,5}))?$#i';

Code: Select all

$val = &quote;905-945-0971x1234&quote;;

Code: Select all

Array
(
    ї0] => 905-945-0971x1234
    ї1] => 
    ї2] => 
    їAreaCode] => 905
    ї3] => 905
    їPrefix] => 945
    ї4] => 945
    їSuffix] => 0971
    ї5] => 0971
    їExt] => 1234
    ї6] => 1234
)
My question is this... Where can I learn about naming the back references (and any other advanced things like it), I have yet to see anything about it in all the sites/tutorials/howto's/etc that i've seen. (and i'm not taking regex in school until next semester ;) )

<edit> phone validation not email :P </edit>

Posted: Fri Jun 17, 2005 3:52 pm
by Chris Corbyn
Wow.... that looks cool :D

I myself have never seen this done - in fact I did not even know it was possible to do. That validation is poor all the same but Wow (did I say that already?)... naming the backreferences like that could be damn useful!

Posted: Fri Jun 17, 2005 4:14 pm
by nickvd
d11wtq wrote:I myself have never seen this done - in fact I did not even know it was possible to do. That validation is poor all the same but Wow (did I say that already?)... naming the backreferences like that could be damn useful!
Neither have I... I'd like to know how it's poor validation, granted, there's a lot that is allowed, however, with the named backreferences, it'll let the programmer re-make the phone number in virtually anyway he/she needs.

Posted: Fri Jun 17, 2005 4:40 pm
by dnathe4th
wow that looks pretty cool. i dont know how it woul dbe used, but if i did, im sure i'd enjoy it :D

Posted: Fri Jun 17, 2005 4:44 pm
by Chris Corbyn
nickvd wrote:
d11wtq wrote:I myself have never seen this done - in fact I did not even know it was possible to do. That validation is poor all the same but Wow (did I say that already?)... naming the backreferences like that could be damn useful!
Neither have I... I'd like to know how it's poor validation, granted, there's a lot that is allowed, however, with the named backreferences, it'll let the programmer re-make the phone number in virtually anyway he/she needs.
Time for d11 to have a play :P

Posted: Fri Jun 17, 2005 8:18 pm
by Chris Corbyn
KK well it's obvious that

Code: Select all

$str = 'I is a string with 123 in me';
preg_match('#(?P<ref_name>\d+)#', $str, $x);
print_r($x);
/*
 Array (
     [ref_name] => 123
     [0] => 123
     [1] => 123
 )

 or something to that effect 
 */
Yields the named back reference although it does still capture the numbered one(s) too.

I should check the Perl documentation for this but I swear I've never seen it.

Posted: Fri Jun 17, 2005 9:49 pm
by Skara
I've read a complete perl regex tutorial (took forever) and never saw anything like this. Awesome.

I think that should return

Code: Select all

Array (
    &#1111;ref_name] => 123
    &#1111;0] => 123
)
The () shouldn't return anything since there's a ?P<> there, I'm guessing.

Posted: Tue Jun 21, 2005 3:55 am
by Weirdan