By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

Regex - How to test for pattern NOT between tags?

P: 72
I've gotten a very good serious of regular expressions to go through C code and add span tags around it to colour code the syntax.

The one thing I can't figure out is how to stop it from putting span tags around something it finds within a block that has previously been marked with span tags.

For example, this regex does the pass for multiline comments:

(?<=[\r\n\s\(])([\/][\*]([^\*]|\n|[\*]+[^\*\/])*[\*]+[\/])(?=[\r\n\s:;,\)])

Expand|Select|Wrap|Line Numbers
  1. /****************
  2.    This is a test.
  3. //  Testing a // testing  /* comment
  4.    test est
  5. ************/
  6.  
Gets marked as:

Expand|Select|Wrap|Line Numbers
  1. <span class="code_multi">/****************
  2.    This is a test.
  3. //  Testing a // testing  /* comment
  4.    test est
  5. ************/</span>
  6.  
Then I do the next pass for one line comments:

(?<=[\r\n\s\(])(\/\/[^\n\r]*)(?=[\r\n\s:;,\)])

Which obviously results in:

Expand|Select|Wrap|Line Numbers
  1. <span class="code_multi">/****************
  2.    This is a test.
  3. <span class="code_oneline">//  Testing a // testing  /* comment</span>
  4.    test est
  5. ************/</span>
  6.  
But I don't want the spans to appear in a section which has already been colour coded. I thought the simple solution was to use a pair of lookahead/lookbehind assertions to check to see if the match fell within an opened but not yet closed span block, but it turns out you can't have non-fixed length strings for the patterns in lookbehind assertions.

(?<!<span class.*(?!<\/span>).*) <insert pattern> (?!.*<\/span>(?<!<span class.*))

Any help would be appreciated.

Regards,
Rob.
Mar 11 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.