9

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 ^.*

1 Answer 1

12

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("^.*\\\\", "");
Sign up to request clarification or add additional context in comments.

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.