Help - Search - Members - Calendar
Full Version: A regex question that's been baffling me.
Invision Power Services > Community Forums > Community Web Design and Coding
_
I've seen this in many applications that use regex, but I can't seem to figure out what it is.
QUOTE
(?:this|that)


What's the ?: there for? I don't see any difference when validating various dummy strings against it. wassat.gif
Chroder
Usually when you create a group, a backreference is also created. For example, if you had the regex (this|that), and it matched, then the backreference would contain "this" or "that", whichever.

The ?: makes the grouping work the same, but tells the regex engine not to create a backreference. See here for more info.

So if you had (this|that)_([0-9]+), the first backreference would contain either "this" or "that", the second would be a number. The expression (?:this|that)_([0-9]+) would only contain one backreference, and that would be the number.
_
Ah, I see now!

CODE
<?php
echo preg_replace("<(?:this|that)_([0-9]+)>is", "$1", "this_3");
?>

Returns '3'.

Thanks! original.gif
DonWilson
QUOTE(Veracon @ May 8 2005, 11:09 AM) *
Ah, I see now!

CODE
<?php
echo preg_replace("<(?:this|that)_([0-9]+)>is", "$1", "this_3");
?>

Returns '3'.

Thanks! original.gif

Are you sure < and > are good brackets? I would suggest matching characters, such as #.
CODE
<?php
echo preg_replace("#(?:this|that)_([0-9]+)#is", "$1", "this_3");
?>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.