Regex - How to test for pattern NOT between tags?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peridian
    New Member
    • Dec 2006
    • 72

    Regex - How to test for pattern NOT between tags?

    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:;,\)])

    Code:
    /****************
       This is a test.
    //  Testing a // testing  /* comment
       test est
    ************/
    Gets marked as:

    Code:
    <span class="code_multi">/****************
       This is a test.
    //  Testing a // testing  /* comment
       test est
    ************/</span>
    Then I do the next pass for one line comments:

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

    Which obviously results in:

    Code:
    <span class="code_multi">/****************
       This is a test.
    <span class="code_oneline">//  Testing a // testing  /* comment</span>
       test est
    ************/</span>
    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.
Working...