Matching \code{foo}

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
ro88o
Forum Newbie
Posts: 2
Joined: Tue Feb 12, 2008 9:00 am
Location: Manchester, UK

Matching \code{foo}

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Matching \code{foo}

Post 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.
ro88o
Forum Newbie
Posts: 2
Joined: Tue Feb 12, 2008 9:00 am
Location: Manchester, UK

Re: Matching \code{foo}

Post 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}.
Post Reply