Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for matching all words between a set of curly braces

Tags:

regex

ruby

A simple question for most regex experts I know, but I'm trying to return all matches for the words between some curly braces in a sentence; however Ruby is only returning a single match, and I cannot figure out why exactly.

I'm using this example sentence:

sentence = hello {name} is {thing}

with this regex to try and return both "{name}" and "{thing}":

sentence[/\{(.*?)\}/]

However, Ruby is only returning "{name}". Can anyone explain why it doesn't match for both words?

like image 615
joeellis Avatar asked Feb 05 '10 21:02

joeellis


1 Answers

You're close, but using the wrong method:

sentence = "hello {name} is {thing}"

sentence.scan(/\{(.*?)\}/)
# => [["name"], ["thing"]]
like image 143
tadman Avatar answered Sep 21 '22 20:09

tadman