在J2SE中使用正则表达式的一个细节

今天在做字符串处理的时候,

发现了在J2SE中使用正则表达式来匹配英文句号的一个小陷阱。

特此记录下来。

 

 

public static void main(String[] args) {
    String test = "aa\\abb.gg\\ad.txt";
    String regEx = ".+\\\\(.+)\\..+$";
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(test);
    boolean rs = m.find();    // 用来验证时候有匹配项
    for (int i = 1; i <= m.groupCount(); i++) {
        System.out.println(m.group(i));
    }
}

 

关键的部分在这一句:

 

String regEx = ".+\\\\(.+)\\..+$";

 

在正则表达式中,我们都知道要匹配特殊字符需要用转义符 “ \ ”  来转移该特殊字符,

所以我相当然的以为跟其他语言一样,

 " \. " 可以匹配英文的句号 " . "

但是在J2SE中,却得到编译器报错的错误。

 

查阅相关资料,java中的dot符号(.)需要用“ \\. ”来转义。

因为Java编译器会把 "\." 当做是对它的字符串对象的转义。

 

原文如下:

It's actually very simple. You need to end up with the escape sequence \. for the regexp to the valid, but it doesn't work because the Java compiler sees it as an escape for its String objects, while a full-stop here does not require escaping.
The work-around is to write "\\.". This way, the backslash is escaped on the first round (remember that a backslash must be escaped anyway), and on the second round (when involving regex), it is the full-stop which is escaped...
 

 

 

========================全文完======================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值