![]() |
Variables in Regular Expession
I have a question of my own! I found out yesterday that you can add so-called variables in your regular expressions, although technically they're more like naming schemes for your array indexes. They can be done like so:
php Code:
Which would output: Code:
Array |
They're called named capturing groups (as opposed to just capturing groups or non-capturing groups). There are a few alternative syntaxes in RegEx-land for doing this, but
(?P<name>...) is all that PHP can use (PCRE). More information is available on the ever useful regular-expressions.info site or in the PHP Manual. As far as I'm aware, the numeric key cannot be omitted from the resulting array of matches. That said it's a trivial matter to parse that array to remove items with a numeric key if you really wanted to. :-) |
An interesting read, Salathe. Thanks for pointing me in the right direction! Before last night I've only managed to read the first few pages of any regular expression documentation. There is some excellent stuff in php.net's manual for regular expressions, such as all the \S, \n.
I know it's not really much of an issue with the numeric items, but I was merely wondering if there was a flag you could set which prevented it. I'm guessing there isn't, though. |
Quote:
|
I guess I should read up on some RegEx too. I started a book on it once, got about 2-3 chapters in and never went back. It's not a subject that particularly interests me, but the power of RegEx cannot be ignored, it is a must for any serious programmer.
|
| All times are GMT. The time now is 11:20 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0