Bug 678705 - java regex does not handle the Pattern.MULTILINE flag properly
Summary: java regex does not handle the Pattern.MULTILINE flag properly
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Fedora
Classification: Fedora
Component: java-1.6.0-openjdk
Version: 13
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: Deepak Bhole
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2011-02-18 22:23 UTC by Lothar Grabowsky
Modified: 2011-02-18 22:58 UTC (History)
8 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2011-02-18 22:58:10 UTC
Type: ---


Attachments (Terms of Use)

Description Lothar Grabowsky 2011-02-18 22:23:24 UTC
Description of problem:

java regex does not handle the Pattern.MULTILINE flag properly.


Version-Release number of selected component (if applicable):

1.6.0.0-50.1.8.7


How reproducible:

According to the documentation of the MULTILINE flag the pattern

"^a.*b$"

should match the string

"xy\naxb"

when the MULTILINE flag is set, because ^ should match the start of the second line in this case (i.e. the position after '\n').
Thus the given input obviously matches the specified pattern. 


Steps to Reproduce:
1. The following little test program can be used to reproduce the issue:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexTest {
   public static void main(String[] argv) {
      Pattern pat = Pattern.compile("^a.*b$", Pattern.MULTILINE);
      Matcher match = pat.matcher("xy\naxb");
      if (match.matches()) {
         System.out.println("match OK");
         System.exit(0);
      } else {
         System.out.println("match FAILED");
         System.exit(1);
      }
   }
}
~                    
 
  
Actual results:

The expression does not match.


Expected results:

The expression should match.


Additional info:

Comment 1 Deepak Bhole 2011-02-18 22:58:10 UTC
Matcher.matches() attempts to match the entire sequence. What you need in the if condition is match.find() instead of match.matches(). 

With match.find(), the pattern matches correctly (and conversely, fails if Pattern.MULTILINE is removed).


Note You need to log in before you can comment on or make changes to this bug.