In mode MATCH_MULTILINE the end-of-line char $ does not work. Test case: RE re = new RE("^a$", RE.MATCH_MULTILINE); System.out.println(re.match("\r\na\r\n")); System.out.println(re.subst("\r\na\r\n", "b", RE.REPLACE_BACKREFERENCES)); Excepted results: true / b Actual results: false / a
I think that match() doesn't work due to bug #4137. I've verified that the problem is gone when suggested fix for 4137 applied. But after this I get exception in subst() Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind ex out of range: -1 at java.lang.String.substring(String.java:1480) at org.apache.regexp.RE.subst(RE.java:1784) at Bug25985.main(Bug25985.java:9) It occured because we specify RE.REPLACE_BACKREFERENCES flag for subst(), but substitution parameter dosn't contain any such references. Thus we try to execute the following line: // Append everything after the last $ sign ret.append(substitution.substring(lLastPosition+2,lLength)); when lLength == 1 and lLastPosition == 0 (so, start position more then end one) To fix this problem we can just change default value of lLastPosition to -2 (so if there is no $ in substitution param we will add substitution itself)
Ok, changing initial value of lLastPosition to -2 fixed exception in case of: r = new RE("^a$"); s = r.subst("a", "b", RE.REPLACE_BACKREFERENCES); But in case when input has new lines result after subst also contains new lines. So this: r = new RE("^a$", RE.MATCH_MULTILINE); s = r.subst("\r\na\r\n", "b", RE.REPLACE_BACKREFERENCES); Results in s == "\r\nb\r\n". Is this expected? Vadim
I think this is correct behavior because after r = new RE("^a$", RE.MATCH_MULTILINE); s = re.match("\r\na\r\n"); s == "a" (neither "\r\na\r\n", "\r\na" or "a\r\n"), and we replace it with "b"
Patches are already applied; closing this bug.
*** Bug 27740 has been marked as a duplicate of this bug. ***