[英]Using backreferences in java regular expressions adjacent to digits
I want to use a regexp replace with back references If the target string is something like: 我想使用带有正向引用的regexp替换如果目标字符串如下所示:
"$1 123"
every thing works fine. 一切正常。 but if I want a target string like:
但是如果我想要一个目标字符串,例如:
"$1123"
It wont work. 它不会工作。
in perl, you can use 在perl中,您可以使用
"${1}123"
To distinguish between the back reference an adjacent numbers - is there a java equivalent? 为了区分反向引用和相邻数字-是否存在Java等效项?
EDIT 编辑
If have time stamps of the format 如果具有以下格式的时间戳记
YYYYMMDDHHMMSS
like: 喜欢:
20130811123000
and I want to reset the hours, minutes, and seconds to get 我想重设小时,分钟和秒以获取
20130811000000
What I would like to do is 我想做的是
String newstring = timestamp.replaceFirst("(\\d{8}))\\d{6}","${1}000000");
您需要将替换指定为:
"$1\\123"
It seems an escape works. 看来逃脱工程。
See this case: 看到这种情况:
String input = "abcd";
System.out.println(input.replaceAll("(a)bcd(e)*?(e)*?(e)*?(e)*?(e)*?(e)*?(e)*?(e)*?(e)*?(e)*?(e)*?", "$1123"));
System.out.println(input.replaceAll("(a)bcd(e)*?(e)*?(e)*?(e)*?(e)*?(e)*?(e)*?(e)*?(e)*?(e)*?(e)*?", "$1\\123"));
Output: 输出:
23
a123
In this case I have an ambiguity between back-referenced group 1 and back-referenced group 11. In the 1st print I don't escape the back-reference, so group 11 is likely to be interpreted, and it's empty. 在这种情况下,我在反向引用组1和反向引用组11之间存在歧义。在第一个打印中,我没有逃脱反向引用,因此组11很可能会被解释,并且为空。 In the 2nd print I escape the replacement after the back-reference to group 1.
在第二张照片中,在回溯到第1组后,我避免了替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.