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
): aSIGNAL
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
): anEND
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