开发者

Jakarta Regexp 1.5 Backreferences?

Why does this match:

String str = "099.9 102.2" + (char) 0x0D;
R开发者_如何学运维E re = new RE("^([0-9]{3}.[0-9]) ([0-9]{3}.[0-9])\r$");        
System.out.println(re.match(str));

But this does not:

String str = "099.9 102.2" + (char) 0x0D;   
RE re = new RE("^([0-9]{3}.[0-9]) \1\r$");      
System.out.println(re.match(str));

The back references don't seem to be working... What am I missing?


Try it with this target string:

"099.9 099.9\r"

A back-reference doesn't mean execute that subexpression again, it means match another instance of whatever that subexpression matched.

You also have to use two backslashes in the back-reference:

RE re = new RE("^([0-9]{3}.[0-9]) \\1\r$");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜