
-----------------------------------
wtd
Sat Nov 13, 2004 11:11 pm

[Regex-tut] Non-Capturing Groups
-----------------------------------
Going way back

Going back to our old example:

/^ \s* ( [hH]ello | [tT]oodles ) ,? \s+ world \s* $/x

Let's say I don't actually want to capture the greeting or parting message, and I'm just using the parentheses to group things.  Why should I capture it and have that capturing group mess up the numbering scheme?

I shouldn't

I should use a non-capturing group instead.  So instead of:

( )

I'll use:

(?: )

Putting it together

/^ \s* (?: [hH]ello | [tT]oodles ) ,? \s+ world \s* $/x

-----------------------------------
templest
Sat Nov 13, 2004 11:42 pm


-----------------------------------
What the hell are all these posts? Could they not have been put into one thread? :eh:

-----------------------------------
Hikaru79
Sat Nov 13, 2004 11:43 pm


-----------------------------------
He wanted to make them short and simple. Easier to digest that way :D Don't complain, just read them ^ ^

-----------------------------------
wtd
Sat Nov 13, 2004 11:45 pm


-----------------------------------
Thanks.  That pretty much sums it up (it helps that Hikaru79 here saw me say that on IRC ;) ).

General Programming doesn't get much traffic anyway, and regular expressions are delightfully general, and not specific to any given language.
