I want to remove everything after last hyphen
branch:resource-fix-c95e12f
I have tried with following command :
replaceAll(".*[^/]*$","");
I want the expected result as branch:resource-fix
I want to remove everything after last hyphen
branch:resource-fix-c95e12f
I have tried with following command :
replaceAll(".*[^/]*$","");
I want the expected result as branch:resource-fix
This expression might simply work:
-[^-]*$
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "-[^-]*$";
final String string = "branch:resource-fix-c95e12f";
final String subst = "";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
final String result = matcher.replaceAll(subst);
System.out.println(result);
Another option would be, ^(.*)-.*$
replaced with \\1
.
The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.