Return first match of Ruby regex

前端 未结 5 1433
甜味超标
甜味超标 2020-12-07 21:26

I\'m looking for a way to perform a regex match on a string in Ruby and have it short-circuit on the first match.

The string I\'m processing is long and from what it

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 22:13

    I am not yet sure whether this feature is awesome or just totally crazy, but your regex can define local variables.

    /\$(?\d+)\.(?\d+)/ =~ "$3.67" #=> 0
    dollars #=> "3"
    

    (Taken from http://ruby-doc.org/core-2.1.1/Regexp.html).

提交回复
热议问题