简体   繁体   中英

Regex For All String Except Certain Characters

I am trying to write a regular expression that matches a certain word that is not preceded by 2 dashes (--) or a slash and a star (/*). I tried some expression but none seem to work.

Below is the text I am testing on

a_func(some_param);

/* a comment initialization */

init;

I am trying to write a regex that will only match the word init in the last line alone, what I've tried so far is matching the word init in initialization and the last line, I tried to look for existing answers, and found that used negative lookahead, but it was still matching init in initialization . Below are the expressions I tried:

  • (?!\\/\\*).*(init)
  • [^(\\-\\-|\\/\\*)].*(init)
  • (?<!\\/\\*).*(init) While reading in regex101's quick reference, I found this negative lookbehind which I believe had a similar example to what I need but I was still not able to get what I want, should I look into the negative lookbehind more or is this not how I achieve what I want?

My knowledge in regular expression is not that extensive, so I don't know if it is possible for what I want or not, but is it doable?

Assuming the -- or /* are on the same line as the init , there are some options. As the commenters said, multiline comments will likely require stronger techniques.

The simplest way I know is to actually preprocess the strings to remove the --.*$ and /\\*.*$ , then look for init (or init\\b if you don't want to match initialization ):

String input = "if init then foo";
String checkme = input.replaceAll("--.*$", "").replaceAll("/\\*.*$", "");
Pattern pattern = Pattern.compile("init");  // or "init\\b"
Matcher matcher = pattern.matcher(checkme);
System.out.println(matcher.find());

You can also use negative lookbehind as in @olsli's answer.

You can start with:

String input = "/*init";
Pattern pattern = Pattern.compile("^((?!--|\\/\\*).)*init");
Matcher matcher = pattern.matcher(input);
System.out.println(matcher.find());

I have added more braces to separate things out. This should also work, tested it in Regexr and IDEONE

Pattern p = Pattern.compile("^(?!=((\\/\\*)|(--)))([.]*?)init[.]*$", Pattern.MULTILINE|Pattern.CASE_INSENSITIVE);
String s = "/* Initialisation";
Matcher m = p.matcher(s);

m.find(); /* should return you >-1 if there's a match

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM