Friday, April 8, 2011

Ruby expression

(?:)

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

Thanks

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

    eric2323223 : @gpojd, How should I understand the '?' and ':' here?
    gpojd : 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.
  • This is an empty, non-capturing group. It has no meaning in this case and can be dropped.

  • 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
    
    Chuck : ?: evaluates to '?' in Ruby 1.9.

0 comments:

Post a Comment