Page 1 of 1

Matching \code{foo}

Posted: Tue Feb 12, 2008 9:10 am
by ro88o
Hi all,

My current project requires me to parse text for things like \foo{bar} and for most things I have a pattern that works, for example to match \emph{text here} I use ~\\\emph\s*{([^}]+)}~ and that works, as it does with many others.

For some of the patterns though I have to include brackets, for example to when trying to match \reservedword{foobar} it would only match when my pattern had brackets round reservedword e.g ~\\\(reservedword)\s*{([^}]+)}~, I don't know if a similar reason is behind my current problem or not, even if it isn't I wouldn't mind somebody explaining why that worked but ~\\\reservedword\s*{([^}]+)}~ didn't.

My current problem is that \code{foobar} isn't being matched with either of the above techniques, both ~\\\code\s*{([^}]+)}~ and ~\\\(code)\s*{([^}]+)}~ don't match it, does anybody either (a) no why or (b) no of a pattern to match it?

Basically I need to match \code followed by a { followed by any character other than } which closes the definition.

Thanks in advance for any help you may be able to offer me,
Tom

Re: Matching \code{foo}

Posted: Tue Feb 12, 2008 9:27 am
by superdezign
Why do you have three slashes at the beginning of your regex? You only need to escape it once.
In other words, you only need two slashes, not three. The third one is escaping the character directly after it.

Re: Matching \code{foo}

Posted: Tue Feb 12, 2008 9:49 am
by ro88o
I originally tried it with 2 and it doesn't match, I have no idea why :( The backslashes in the text I'm matching aren't escaped either so there's no reason for me to use 3 backslashes to match 1, like I said it just works!

I thought that could be a problem for matching code so I removed a backslash but it still didn't match \code{foo}.