Bug 25985 - In MATCH_MULTILINE-mode $ does not match end of line
Summary: In MATCH_MULTILINE-mode $ does not match end of line
Alias: None
Product: Regexp
Classification: Unclassified
Component: Other (show other bugs)
Version: unspecified
Hardware: Other other
: P3 normal (vote)
Target Milestone: ---
Assignee: Jakarta Notifications Mailing List
: 27740 (view as bug list)
Depends on: 4137
  Show dependency tree
Reported: 2004-01-08 12:55 UTC by Hendrik Brummermann
Modified: 2004-11-16 19:05 UTC (History)
1 user (show)


Note You need to log in before you can comment on or make changes to this bug.
Description Hendrik Brummermann 2004-01-08 12:55:13 UTC
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.subst("\r\na\r\n", "b", RE.REPLACE_BACKREFERENCES));

Excepted results:
true / b

Actual results:
false / a
Comment 1 Oleg Sukhodolsky 2004-01-09 04:15:20 UTC
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
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)
Comment 2 Vadim Gritsenko 2004-01-31 00:15:22 UTC
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?

Comment 3 Oleg Sukhodolsky 2004-02-10 15:56:54 UTC
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"
Comment 4 Vadim Gritsenko 2004-02-27 02:39:28 UTC
Patches are already applied; closing this bug.
Comment 5 Vadim Gritsenko 2004-03-18 18:45:40 UTC
*** Bug 27740 has been marked as a duplicate of this bug. ***