How to match only after an explicit signal word in regex?

membersound :

I want to match any "nonchar + digits" between a SIGNAL and an END word.

(?!SIGNAL)\\W+\\d+(?=END)

BUT: the following matches +2 and ++7:

random+2END+SIGNAL+random++7END

Why is the +2 matched here? I only would want the ++7 here.

My final goal is to replace the match with blanks.

Example: https://regexr.com/4727h

Java code:

Pattern.compile(REGEX).matcher(input).replaceFirst(StringUtils.EMPTY);
Wiktor Stribiżew :

You may use

s.replaceFirst("(SIGNAL.*?)\\W+\\d+(END)", "$1$2")

The regex matches:

  • (SIGNAL.*?) - Capturing group 1 ($1): a SIGNAL substring and then any 0+ chars other line break chars, as few as possible (as *? is a non-greedy, reluctant quantifier)
  • \W+ - 1 or more non-word chars (chars other than letters, digits and _)
  • \d+ - 1+ digits
  • (END) - Capturing group 2 ($2): an END substring
  • $1$2 - two numeric replacement backreferences that refer to Group 1 and Group 2 values

See the Java demo:

String s = "random+2END+SIGNAL+random++7END";
System.out.println(s.replaceFirst("(SIGNAL.*?)\\W+\\d+(END)", "$1$2"));
// => random+2END+SIGNAL+randomEND

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161375&siteId=1