开发者社区> 问答> 正文

java regex?报错

@Test
	public void test() {
		String ss = "agb\\sdAFDSDGs\\gas123.txt";

		Pattern p = Pattern.compile("\\");

		Matcher m = p.matcher(ss);

		m.replaceAll("/");
	}

运行报错:

java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
 ^
	at java.util.regex.Pattern.error(Pattern.java:1924)
	at java.util.regex.Pattern.compile(Pattern.java:1671)
	at java.util.regex.Pattern.<init>(Pattern.java:1337)
	at java.util.regex.Pattern.compile(Pattern.java:1022)
	at org.happymzw.litter.file.PrintFileName.test(PrintFileName.java:53)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

想要实现的效果:把一个字符串中的 "\" 全部替换为 "/"

Help  ~~!





展开
收起
爱吃鱼的程序员 2020-06-10 10:59:18 531 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    自己解决了,

    修改如下:

    \\Patternp=Pattern.compile("\\");Patternp=Pattern.compile("\\\\");



    '\'在java中是一个转义字符,所以需要用两个代表一个。例如System.out.println("\\");只打印出一个"\"。但是'\'也是正则表达式中的转义字符(replaceAll的参数就是正则表达式),需要用两个代表一个。所以: \\\\ 被java转换成 \\,\\ 又被正则表达式转换成\。


    http://blog.csdn.net/sunlovefly2012/article/details/16807761

    2020-06-10 10:59:35
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载