java:String正则表达式替换时对$符号的特别处理塈异常IllegalArgumentException:Illegal group reference

String类型提供了正则表达式替换方法,如:replaceFirst,replaceAll,它们其实是对应调用
java.util.regex.Matcher的同名方法
如果你仔细看它们的方法说明就会看到

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string;
请注意,替换字符串中的反斜杠(\)和美元符号($)可能会导致结果与将其视为文字替换字符串时的结果不同;

Illegal group reference

也就是说反斜杠(\)和美元符号($)在替换字符中有特别含义。比如$1在替换字符串中代表表达式匹配的捕获组1(capture group),在替换过程中如果遇到$符号,后面就必须是个0-9数字,否则就会抛出异常IllegalArgumentException:Illegal group reference

如下的代码中替换数据中为${hello}就会复现此异常:


import static org.junit.Assert.assertTrue;

import java.util.regex.Matcher;
import org.junit.Test;

public class RegexTest {
	public static final String NEW_LINE = System.getProperty("line.separator");
	private static final String commentBody = "/**" + NEW_LINE + " */" + NEW_LINE;
	@Test
	public void testReplaceFirst() {
		try {
			String comment = "$(hello)";
			/** 在第一个换行符后面增加${hello} */
			String cmt = commentBody.replaceFirst(NEW_LINE, "$0" + comment);
			System.out.println(cmt);				
		} catch (Exception e) {
			e.printStackTrace();
			assertTrue(false);
		}
	}
}

Matcher.quoteReplacement

Matcher中提供了quoteReplacement静态方法用于将替换字符串中的\$符号转义
要避免上面的问题,就可以如下在替换前将${hello}内容调用quoteReplacement方法转义处理就不会再抛出异常

import static org.junit.Assert.assertTrue;

import java.util.regex.Matcher;
import org.junit.Test;

public class RegexTest {
	public static final String NEW_LINE = System.getProperty("line.separator");
	private static final String commentBody = "/**" + NEW_LINE + " */" + NEW_LINE;
	@Test
	public void testReplaceFirst() {
		try {
			String comment = "$(hello)";
			System.out.println(Matcher.quoteReplacement(comment));
			/** 在第一个换行符后面增加${hello} */
			String cmt = commentBody.replaceFirst(NEW_LINE, "$0" + Matcher.quoteReplacement(comment));
			System.out.println(cmt);				
		} catch (Exception e) {
			e.printStackTrace();
			assertTrue(false);
		}
	}
}

输出

$(hello)
/**
$(hello) */

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值