tags:

views:

180

answers:

3
+1  Q: 

Ruby expression

(?:)

It is a valid ruby regular expression, could anyone tell me what it means?

Thanks

+3  A: 

It will not capture the part of the matching string in a backreference (i.e \1).

gpojd
@gpojd, How should I understand the '?' and ':' here?
eric2323223
For example, (\d+) will capture consecutive digits in a backreference like \1. If you want to group part of the regex, but do not want to capture them, you would use (?:\d+). Needlessly capturing the data can decrease performance.
gpojd
+2  A: 

This is an empty, non-capturing group. It has no meaning in this case and can be dropped.

Gumbo
+9  A: 

Like others have said, it's used as the non-capturing syntax for a regex, but, it's also valid ruby syntax outside of a regex.

In ruby ?: is the integer value for the colon character:

% irb
irb> ?:
=> 58
irb ":"[0]
=> 58

Adding parenthesis doesn't change the value: (?:) == ?:

When you add spaces (? :), it's the ternary operator, which is essentially shorthand for if/then/else in ruby, so the statement ( bool ? truish : falsy ) is equivalent to

if bool then 
  truish 
else 
  falsy 
end
rampion
?: evaluates to '?' in Ruby 1.9.
Chuck