views:

45

answers:

1

Why does this match:

String str = "099.9 102.2" + (char) 0x0D;
RE 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?

+2  A: 

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$");
Alan Moore
That was it! Thanks Alan.
msi