java反斜杠和双反斜杠不同,Java Regexp模式有双反斜杠,如何以可读格式存储模式...

Would be great to have convenient way of storing patterns with single backslash. Some workarounds: store it in the file and use NIO to read. Cons: Java EE does not allow IO access. Store somehow in JNDI. Maybe new to java 5 Pattern.LITERAL flag can help? I want to work with normal pattern string, like \d, not \\d.

解决方案

the trouble is that \ is a special char in java when creating a String, regardless of regexp or not.

eg String s = "\t";

you cannot use this for arbitrary chars though, String s = "\a"; will give you a compile-time error. the valid chars are b, t, n, f, r, ", ' and \

therefore to get a literal \ in a string in java you need to escape it like so : \\. because of that, your only option is to NOT have these strings in java files, hence in an external file that is loaded by your java file. Pattern.LITERAL won't help at all because you still need a valid java string, which \d isn't.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值