Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String.replaceAll regex

Tags:

java

string

regex

What is the regex to strip the MY-CORP\ part of na inputed string like MY-CORP\My.Name with the java String.replaceAll method so I can get only the My.Name part?

I tried

public static String stripDomain(String userWithDomain) {
    return userWithDomain.replaceAll("^.*\\", "");
}

but i got Unexpected internal error near index 4 ^.*

like image 751
Azder Avatar asked Feb 04 '23 11:02

Azder


1 Answers

Your problem is that the backslash has special meaning both in Java strings and in regexes. So you need four slashes in the Java source code, passing two to the regex parser to get one literal one in the regex:

return userWithDomain.replaceAll("^.*\\\\", "");
like image 123
Michael Borgwardt Avatar answered Feb 06 '23 10:02

Michael Borgwardt