eric2323223
eric2323223

Reputation: 3628

Ruby expression

(?:)

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

Thanks

Upvotes: 1

Views: 280

Answers (3)

rampion
rampion

Reputation: 89123

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

Upvotes: 9

Gumbo
Gumbo

Reputation: 655765

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

Upvotes: 2

gpojd
gpojd

Reputation: 23085

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

Upvotes: 3

Related Questions