Regular expression for a string that does not star

2019-08-03 16:29发布

问题:

I use EditPad Pro text editor. I need read string into code, but I need to ignore ones that start with the label "/*" or tab + /*, for example:

/**
 * Light up the dungeon using "claravoyance"
 *
 * memorizes all floor grids too.
**/ 
/** This function returns TRUE if a "line of sight" **/
#include "cave.h"
 (tab here) /* Vertical "knights" */

if (g->multiple_objects) {
  /* Get the "pile" feature instead */
  k_ptr = &k_info[0];
}

put_str("Text inside", hgt - 1, (wid - COL_MAP) / 2);

/* More code*** */

I like to return:

"Text inside"

I have try this (reading Regular expression for a string that does not start with a sequence), but not work for me:

^(?! \*/\t).+".*"

any help?

Edit: I used:

^(?!#| |(\t*/)|(/)).+".*"

And it return:

put_str("Text inside"

I'm close to finding the solution.

回答1:

EditPad obviously supports variable-length lookbehind in pro version 6 and lite version 7 since it's flavor is indicated as "JGsoft": Just Great Software regular expression engine.

Knowing this and without the use of capture groups, you could combine two variable length lookbehinds:

(?<!^[ \t]*/?[*#][^"\n]*")(?<=^[^"\n]*")[^"]+
  • (?<!^[ \t]*/?[*#][^"\n]*") The negative lookbehind for avoiding the quoted part to be preceded by [ \t]*/?[*#] any comments, which could be preceded by any amount of space/tab. Made the / optional, as a multi-line comment can also start with *.
  • (?<=^[^"\n]*") The positive lookbehind for assuring, that there's any amount of [^"\n], characters, that are no quotes or newlines followed by one quote before.
  • [^"]+ As supposed to be always balanced quoting, now it should be convenient, to match the non-quotes after the first double-quote (which is inside the lookbehind)
  • If a single " may occur in any line (not balanced), change the end: [^"]+ to [^"\n]+(?=")

Possibly there are different solutions for the problem. Hope it helps :)



回答2:

Here's one approach: ^(?!\t*/\*).*?"(.+?)"

Breakdown:

^(?!\t*/\*)  This is a negative lookahead anchored to the beginning of the line, 
             to ensure that there is no `/*` at the beginning (with or 
             without tabs)

.*?"         Next is any amount of characters, up to a double-quote. It's lazy 
             so it stops at the first quote


(.+?)"       This is the capture group for everything between the quotes, again
             lazy so it doesn't slurp other quotes


回答3:

You can use this regex:

/\*.*\*/(*SKIP)(*FAIL)|".*?"

Working demo

Edit: if you use EditPad then you can use this regex:

"[\w\s]+"(?!.*\*/)


标签: regex editpad