views:

63

answers:

1

I'm working on a website where I have some text which I am using the RedCloth gem to markdown. basically I need to intercept some text before it gets to the gem, and parse the text with a colour code syntax gem. My regex is very poor (and I don't know if I can do this.) I need to select chucks of text and parse them before RedCloth gets ahold of it.

example text

I need to select (in ruby) the chucks of text that start with ##code(language) and end with #code, I also need to know what is inside of the bracket at the start tag ##code(in-here)

other text.....

##code(ruby)

puts "Hello World"

##code

more text.......


##code(c++)

#include <string>
#include "logger.h"

##code

etc...

any help would be greatly appreciated.

Thanks Phil.

A: 

This should work for you:

/##code\((\S+)\)(.+?)##code/m

That'll match any non-space character, so as long as your (language) stuff doesn't have spaces, you'll be good to go.

jvenema
/##code\((\S+)\)(.+)##code/m He needs to match what's inside the (language) pattern as well.
Myrddin Emrys
and add '?' - /##code\((\S+)\)(.+?)##code/m
aaz
Updated per suggestions. Thanks guys.
jvenema
thanks guys, I'm gonna have to pickup a book on this.
PhilCK
is it possible to stop it removing slashes?>> "##code(ruby) \\\\ ##code".sub(/##code\((\S+)\)(.+?)##code/m, "\\\\")=> "\\"not sure why the back slashes are being removed
PhilCK
Might want to ask a new question about that one
jvenema