1

I have a problem with creating regex of match that will get from string example: NotificationGroup_n+En where n are numbers from 1-4 and when let's say i match desired number from range i will replace or remove it with that specific number.

String BEFORE process: NotificationGroup_4+E3

String AFTER process: NotificationGroup_E3

I removed n (number from 1-4) and leave _E with number

My question is how to write regex in string.replace function to match number and than the plus sign and leave out only the string with _En

def String string = "Notification_Group_4+E3";
    println(removeChar(string));
}
public static def removeChar(String string) { 
    if ((string.contains("1+"))||(string.contains("2+")||(string.contains("3+"))||(string.contains("4+")))) {
        def stringReplaced = string.replace('4+', "");
        return stringReplaced;
    }
}

3 Answers 3

3

in groovy:

def result = "Notification_Group_4+E3".replaceFirst(/_\d\+(.*)/, '_$1')
println result

output:

~>  groovy solution.groovy
Notification_Group_E3

~> 

Try it online!

A visualization of the regex look like this:

regex visualization

Regex explanation:

  1. we use groovy slashy strings /.../ to define the regex. This makes escaping simpler
  2. we first match on underscore _
  3. Then we match on a single digit (0-9) using the predefined character class \d as described in the javadoc for the java Pattern class.
  4. We then match for one + character. We have to escape this with a backslash \ since + without escaping in regular expressions means "one or more" (see greedy quantifiers in the javadocs) . We don't want one or more, we want just a single + character.
  5. We then create a regex capturing group as described in the logical operators part of the java Pattern regex using the parens expression (.*). We do this so that we are not locked into the input string ending with E3. This way the input string can end in an arbitrary string and the pattern will still work. This essentially says "capture a group and include any character (that is the . in regex) any number of times (that is the * in regex)" which translates to "just capture the rest of the line, whatever it is".
  6. Finally we replace with _$1, i.e. just underscore followed by whatever the capturing group captured. The $1 is a "back reference" to the "first captured group" as documented in, for example, the java Matcher javadocs.
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for support, finally understand regex little bit better, appreciated
1

try this regex (\d.*?\+) here demo

in java :

String string = "Notification_Group_4+E3";
System.out.print(string.replaceAll("\\d.*?\\+", ""));

output :

Notification_Group_E3

1 Comment

This solution helped me a lot, thanks, nice site to test regex
0

The simple one-liner:

String res = 'Notification_Group_4+E3'.replaceAll( /_\d+\+/, '_' )
assert 'Notification_Group_E3' == res

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.