java 匹配反斜杠_Java Language

Example

If you want to match a backslash in your regular expression, you'll have to escape it.

Backslash is an escape character in regular expressions. You can use '\\' to refer to a single backslash in a regular expression.

However, backslash is also an escape character in Java literal strings. To make a regular expression from a string literal, you have to escape each of its backslashes. In a string literal '\\\\' can be used to create a regular expression with '\\', which in turn can match '\'.

For example, consider matching strings like "C:\dir\myfile.txt". A regular expression ([A-Za-z]):\\(.*) will match, and provide the drive letter as a capturing group. Note the doubled backslash.

To express that pattern in a Java string literal, each of the backslashes in the regular expression needs to be escaped.

String path = "C:\\dir\\myfile.txt";

System.out.println( "Local path: " + path ); // "C:\dir\myfile.txt"

String regex = "([A-Za-z]):\\\\.*"; // Four to match one

System.out.println("Regex: " + regex ); // "([A-Za-z]):\\(.*)"

Pattern pattern = Pattern.compile( regex );

Matcher matcher = pattern.matcher( path );

if ( matcher.matches()) {

System.out.println( "This path is on drive " + matcher.group( 1 ) + ":.");

// This path is on drive C:.

}

If you want to match two backslashes, you'll find yourself using eight in a literal string, to represent four in the regular expression, to match two.

String path = "\\\\myhost\\share\\myfile.txt";

System.out.println( "UNC path: " + path ); // \\myhost\share\myfile.txt"

String regex = "\\\\\\\\(.+?)\\\\(.*)"; // Eight to match two

System.out.println("Regex: " + regex ); // \\\\(.+?)\\(.*)

Pattern pattern = Pattern.compile( regex );

Matcher matcher = pattern.matcher( path );

if ( matcher.matches()) {

System.out.println( "This path is on host '" + matcher.group( 1 ) + "'.");

// This path is on host 'myhost'.

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值