37

I have come across a regular expression that I don't fully understand - can somebody help me in deciphering it:

^home(?:\/|\/index\.asp)?(?:\?.+)?$

It is used in url matching and the above example matches the following urls:

home
home/
home/?a
home/?a=1
home/index.asp
home/index.asp?a
home/index.asp?a=1

It seems to me that the question marks within the brackets (?: don't do anything. Can somebody enlighten me.

The version of regex being used is the one supplied with Classic ASP and is being run on the server if that helps at all.

Graham
  • 7,807
  • 20
  • 69
  • 114
  • 5
    The regex bible site is [regular-expressions.info](http://regular-expressions.info/refadv.html). It has everything you need to know about regex and great explanations. – Bohemian Jan 03 '13 at 11:43
  • @Bohemian Non-capture group `(?:regex)` is listed on page https://www.regular-expressions.info/refcapture.html – NZD Jun 12 '23 at 02:37

4 Answers4

48

(?:) creates a non-capturing group. It groups things together without creating a backreference.

A backreference is a part you can refer to in the expression or a possible replacement (by saying \1 or $1 etc - depending on flavor). You can also extract them from a match afterwards when using regex in a programming language. The main reason for using (?:) is to avoid creating a new backreference, which avoids incrementing the group number which is especially important if you're repeating a group and do not want to have unpredictable group numbers, and saves (a usually negligible amount of) memory

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
  • It means that it allows grouping (in your example this is useful for having the alternatives with |) while not creating a __capturing__ group (something you can refer to elsewhere or extract from your successful match) – ebottard Jan 03 '13 at 11:39
  • Some regular expression functions let you grab a particular group out of the regular expression, e.g., the first parenthesized group, the second, etc. If you use the non-capturing, it doesn't add to the numbering and can't be grabbed as a separate group. (It may be a little faster also.) – Brett Zamir Jan 03 '13 at 11:39
  • Sometimes it's imperative to avoid capturing bracketed groups. Grouped matches can be repeated (for example, using `+` or `*` outside the brackets), creating an unknown number of captured groups. It then becomes tricky to backreference subsequent matches in the input as their numbering is indeterminate at the time of writing the regex. Using non-capturing groups for repetition avoids this. – Bob Sammers Feb 27 '23 at 13:47
15

It's a non-capture group, which essentially is the same as using (...), but the content isn't retained (not available as a back reference).

If you're doing something like this: (abc)(?:123)(def) You'll get abc in $1 and def in $2, but 123 will only be matched.

Mario
  • 35,726
  • 5
  • 62
  • 78
2

From documentation:

(?:...)
A non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern.
keramat
  • 4,328
  • 6
  • 25
  • 38
1

its really easy every parentheses will create a variable in the memory so you can use the parentheses value afterward so to not save it in memory just put :? in the parentheses like this (?:) and then fill the rest as you need. that's it and nothing else

Vfx Master
  • 27
  • 1
  • 7