Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a capturing regex repeat

Tags:

regex

ruby

I have a regex \[\[(.+)\]\]. I use it to capture wikilinks like hey I [[am]] so awesome (captures am). How could I modify this so that hey I [[am]] more [[awesome]] than you think yields both am and awesome, separately? My attempts have yielded single strings like am]] more [[awesome. A little context: I'm using this to write a Ruby IRC bot.

P.S. Wikilinks can also be multi-word, like hey I am much [[more awesome]] than you [[probably think]].

like image 637
Kudu Avatar asked Dec 01 '22 02:12

Kudu


1 Answers

You need to make the .+ non-greedy:

\[\[(.+?)\]\]

See this reference: ruby regexen.

like image 117
Mat Avatar answered Dec 09 '22 21:12

Mat