After reading some tutorials I still don't get it.
Could someone explain how ?: is used and what it's good for?
After reading some tutorials I still don't get it.
Could someone explain how ?: is used and what it's good for?
?:
is used when you want to group an expression, but you do not want to save it as a matched/captured portion of the string.
An example would be something to match an IP address:
/(?:\d{1,3}\.){3}\d{1,3}/
Note that I don't care about saving the first 3 octets, but the (?:...)
grouping allows me to shorten the regex without incurring the overhead of capturing and storing a match.
Groups that capture you can use later on in the regex to match OR you can use them in the replacement part of the regex. Making a non-capturing group simply exempts that group from being used for either of these reasons.
Non-capturing groups are great if you are trying to capture many different things and there are some groups you don't want to capture.
Thats pretty much the reason they exist. While you are learning about groups, learn about Atomic Groups, they do a lot! There is also lookaround groups but they are a little more complex and not used so much.
Example of using later on in the regex (backreference):
<([A-Z][A-Z0-9]*)\b[^>]*>.*?</\1>
[ Finds an xml tag (without ns support) ]
([A-Z][A-Z0-9]*)
is a capturing group (in this case it is the tagname)
Later on in the regex is \1
which means it will only match the same text that was in the first group (the ([A-Z][A-Z0-9]*)
group) (in this case it is matching the end tag).
It makes the group non-capturing, which means that the substring matched by that group will not be included in the list of captures. An example in ruby to illustrate the difference:
"abc".match(/(.)(.)./).captures #=> ["a","b"]
"abc".match(/(?:.)(.)./).captures #=> ["b"]
You can use capturing groups to organize and parse an expression. A non-capturing group has the first benefit, but doesn't have the overhead of the second. You can still say a non-capturing group is optional, for example.
Say you want to match numeric text, but some numbers could be written as 1st, 2nd, 3rd, 4th,... If you want to capture the numeric part, but not the (optional) suffix you can use a non-capturing group.
([0-9]+)(?:st|nd|rd|th)?
That will match numbers in the form 1, 2, 3... or in the form 1st, 2nd, 3rd,... but it will only capture the numeric part.
Let me try to explain this with an example:
Consider the following text:
http://stackoverflow.com/
http://stackoverflow.com/questions/tagged/regex
Now, if I apply the regex below over it...
(http|ftp)://([^/\r\n]+)(/[^\r\n]*)?
... I would get the following result:
Match "http://stackoverflow.com/"
Group 1: "http"
Group 2: "stackoverflow.com"
Group 3: "/"
Match "http://stackoverflow.com/questions/tagged/regex"
Group 1: "http"
Group 2: "stackoverflow.com"
Group 3: "/questions/tagged/regex"
But I don't care about the protocol. I just want the host and path of the URL. So, I change the regex to include the non-capturing group (?:)
.
(?:http|ftp)://([^/\r\n]+)(/[^\r\n]*)?
Now, my result looks like this:
Match "http://stackoverflow.com/"
Group 1: "stackoverflow.com"
Group 2: "/"
Match "http://stackoverflow.com/questions/tagged/regex"
Group 1: "stackoverflow.com"
Group 2: "/questions/tagged/regex"
See? The first group has not been captured. The parser uses it to match the text, but ignores it later, in the final result.
Hope it helps. Sorry for my lousy english.
As requested, let me try to explain groups too.
Well, groups serve many purposes. They help you extract exact information from a bigger match (which can also be named), let you rematch a previous matched group, and can be used for substitutions. Lets try some examples, shall we?
Ok, imagine you have some kind of XML or HTML (be aware that regex may not be the best tool for the job, but it is nice as an example). You want to parse the tags, so you could you something like this (I have added spaces to make it easier to understand):
\<(?<TAG>.+?)\> [^<]*? \</\k<TAG>\>
or
\<(.+?)\> [^<]*? \</\1\>
The first regex has a named group (TAG), while the second one uses a common group. Both regexes do the same thing: they use the value from the first group (the name of the tag) to match the closing that. The difference is that the first one uses the name to use the value, and the second one uses the group index (which starts at 1).
Lets try some substitutions now. Consider the following text:
Lorem ipsum dolor sit amet consectetuer feugiat fames malesuada pretium egestas.
Now, lets use the this dumb regex over it:
\b(\S)(\S)(\S)(\S*)\b
This regex matches words with at least 3 characters, and uses groups to separate the first three letters. The result is this:
Match "Lorem"
Group 1: "L"
Group 2: "o"
Group 3: "r"
Group 4: "em"
Match "ipsum"
Group 1: "i"
Group 2: "p"
Group 3: "s"
Group 4: "um"
...
Match "consectetuer"
Group 1: "c"
Group 2: "o"
Group 3: "n"
Group 4: "sectetuer"
...
So, if we apply the substitution string...
$1_$3$2_$4
... over it, we are trying to use the first group, add an underscore, use the third group, then the second group, add another underscore, and then the fourth group. The resulting string would be like the one below.
L_ro_em i_sp_um d_lo_or s_ti_ a_em_t c_no_sectetuer f_ue_giat f_ma_es m_la_esuada p_er_tium e_eg_stas.
You can use named groups form substitutions too, using ${name}.
To play around with regexes, I recommend Rad Software Regular Expression Designer, which has a nice "Language Elements" tab with quick access to some basic instructions. It's based at .NET's regex engine.
Hope I've help.