Java String.replaceAll正则表达式 [英] Java String.replaceAll regex

查看:350
本文介绍了Java String.replaceAll正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是使用java String.replaceAll 方法剥离MY-CORP \部分的输入字符串的正则表达式,例如MY-CORP\My.Name我只能获得My.Name部分?

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?

我试过

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

但是我在索引4 ^附近发现了意外的内部错误。 *

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

推荐答案

您的问题是反斜杠在Java字符串和正则表达式中都有特殊含义。因此,您需要Java源代码中的四个斜杠,将两个传递给正则表达式解析器以在正则表达式中获得一个字面值:

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("^.*\\\\", "");

这篇关于Java String.replaceAll正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆