简体   繁体   English

Java String.replaceAll正则表达式

[英]Java String.replaceAll 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? 什么是使用java String.replaceAll方法剥离MY-CORP \\ na的输入字符串的部分,如MY-CORP \\ My.Name所以我只能得到My.Name部分?

I tried 我试过了

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

but i got Unexpected internal error near index 4 ^. 但我在索引4 ^附近遇到了意外的内部错误。 * *

Your problem is that the backslash has special meaning both in Java strings and in regexes. 您的问题是反斜杠在Java字符串和正则表达式中都有特殊含义。 So you need four slashes in the Java source code, passing two to the regex parser to get one literal one in the regex: 因此,您需要Java源代码中的四个斜杠,将两个传递给正则表达式解析器以在正则表达式中获取一个文字:

return userWithDomain.replaceAll("^.*\\\\", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM