Eric
Eric

Reputation: 5331

What does ?: mean?

In an Apache configuration file, there is a line to avoid gzip compression for images:

SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

I do not believe there could exist a .gif file name like myfile.*:gif.

So what does ?: mean?

Upvotes: 3

Views: 801

Answers (1)

jjnguy
jjnguy

Reputation: 138864

This specific part (?:gif|jpe?g|png) is almost the exact same things as (gif|jpe?g|png).

The difference here is that the first one will not 'capture' the group in parenthesis. In other words, when looking at the matches, the stuff in the first example will not be included as a special group.

Normally when you enclose items in parenthesis it is called a 'capture group.' In other words, when looking at the results of matching the regex with text, the stuff in the parenthesis will be put into a 'group' to be examined later. When you use (?:stuff) the stuff inside the parenthesis is still grouped together in the regex, but it is not considered a capture group.

This can be helpful if you want to match the filename of a bunch of images, bot not capture more than the filename:

"([^.]\.)(?:gif|png|tiff)"

When matching a file against this regex, only the filename will be captured. The file extension will not be because it is not in a capture group.

Upvotes: 10

Related Questions