Steve Hansell
Steve Hansell

Reputation: 17703

Finding content between multiple start and end tags using regular expression

I'm trying to run a regular expression (Ruby) on a file containing code and custom comment tags. I want to find all text between (/*+ ... +*/), single line or multiline.

Given:

/*+
   # Testing Documentation #
   ## Another Documentation Line ##
   This is also picked up
+*/
Some code here that is ignored
/*+ # More documentation # +*/

I would want to match each group of text between the open and closing of /*+ ... +*/

I've tried the following reg ex which works great for the single line example. But if I enable the multiline option, it picks up everything between the first match and last match instead of matching two or more groups.

/(?<=\/\*\+)(.*)(?=\+\*\/)/

Thanks

Upvotes: 1

Views: 1020

Answers (2)

Michael Kohl
Michael Kohl

Reputation: 66837

Make the match in the middle non-greedy ((.*?)). Here's a permalink to Rubular:

http://rubular.com/r/0SuNNJ3vy6

The expression I used was /(\/\*\+)(.*?)(\+\*\/)/m, fine-tune as you see fit.

text.scan(/(\/\*\+)(.*?)(\+\*\/)/m).map(&:join)
#=> ["/*+\n   # Testing Documentation #\n   ## Another Documentation Line ##\n   This is also picked up\n+*/", "/*+ # More documentation # +*/"]

Upvotes: 4

quzary
quzary

Reputation: 285

Maybe that'll help you. This works in JS:

/(?:\/\*\+)([\w\W]*?)(?=\+\*\/)/igm

Upvotes: 0

Related Questions