Home Download Buy Blog Forum Support

Help with syntax-matching across multiple lines

Help with syntax-matching across multiple lines

Postby bees on Mon Dec 31, 2012 8:14 am

I am writing a language highlighting syntax and have run into a bit of a wall. I have a "begin-end" syntax pattern, and inside it are a number of similar lines (designated by a specific terminating character). I'd like to match only the first one of these, but not any others. Example:

Code: Select all
this line I want to match, since it ends with a#
but this line I don't care about#
nor this one#
even though they're matched by the same pattern#

then more unrelated exciting stuff happens below
which other patterns will tend to
oh, and don't match this line either#

Using a simple interior pattern ^.*#$ matches every #-terminated line. To limit it, I tried adding a lookbehind to see if the line in question was preceded by the begin pattern (which would make it the first), and I also tried incorporating the entire first #-terminated line into the "begin" pattern pattern itself, but it looks like neither of those work since maching seems to be limited to one line at a time.

What I think is my only other option is to limit that interior pattern to matching a fixed number of times (for this case, just once). Is such a thing possible? Or is there another way I could match this?

Thank you.
Posts: 1
Joined: Mon Dec 31, 2012 5:33 am

Re: Help with syntax-matching across multiple lines

Postby thebreiflabb on Fri Jan 04, 2013 9:42 am

I am in no way a regex expert, so it might not work. But it seems to give the wanted results? Notice the m flag.

Code: Select all
/^BEGINNING PATTERN(:?.|\s)*?(^.*#$){1}(:?.|\s)*?ENDING PATTERN$/mg
Posts: 3
Joined: Wed Jan 02, 2013 11:39 am

Return to General Discussion

Who is online

Users browsing this forum: Exabot [Bot] and 7 guests