Return first match of Ruby regex

前端 未结 5 1428
甜味超标
甜味超标 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:07

    You could try variableName[/regular expression/]. This is an example output from irb:

    irb(main):003:0> names = "erik kalle johan anders erik kalle johan anders"
    => "erik kalle johan anders erik kalle johan anders"
    irb(main):004:0> names[/kalle/]
    => "kalle"
    

提交回复
热议问题