使用replaceAll和正则表达式前置字符串
我不知道如何使用正向前瞻创建正则表达式。这个想法是在长字符串中的每两个字符前面添加两个字符串。即
"090909" => "XX09XX09XX09"
这段代码:
String s = "090909";
String ns = s.replaceAll("(?=\\d\\d)", "XX");
...不起作用;输出为XX0XX9XX0XX9XX09
。但这段代码有效:
String s = "090909";
String ns = s.replaceAll("(?=09)", "XX");
我对如何想出一个表达式来表示每两个字符的前瞻感到困惑。我是否遗漏了一些界限或其他什么?
I can't figure out how to create regular expression using positive lookahead. The idea is to prepend two character string to every two character in a long string. i.e.
"090909" => "XX09XX09XX09"
This code:
String s = "090909";
String ns = s.replaceAll("(?=\\d\\d)", "XX");
...doesn't work; the output is XX0XX9XX0XX9XX09
. But this code works:
String s = "090909";
String ns = s.replaceAll("(?=09)", "XX");
I'm confused on how to come up with an expression saying lookahead for every two characters. Am I missing some boundaries or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用以下内容:
(
和)
将创建 捕获,$1
访问捕获。You can use the following:
The
(
and)
will create the capture, and the$1
accesses the capture.