Matching Line Boundaries in a Regular Expression (Pattern.MULTILINE/(?m)) is broken in Java?

The example on gives false for me twice but should'nt! Why?

CharSequence inputStr = "abc\ndef"; String patternStr = "abc$"; // Compile with multiline enabled Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.find(); // true // Use an inline modifier to enable multiline mode matchFound = pattern.matches(".*abc$.*", "abc\r\ndef"); // false System.out.println(matchFound); // false matchFound = pattern.matches("(?m).*abc$.*", "abc\r\ndef"); // true System.out.println(matchFound);// false !!!!!

Well, if I put an additionally (?s) it works, but shouldn't it work without the (?s)? Did this behaviour change in the past or didn't the authors simply check there example?

-------------Problems Reply------------

First of all, is an extremely bad site: never ever assume anything found there as being "the truth".

In regex, the $ never matches a character, it matches a position. In (?m) mode, it matches the "empty string" before a line break, or the end of the string. So given the string "abc\r\ndef" the regex ".*abc$.*" does not match since the \r\n isn't present in your regex. The $ matches the position between c and \r.

What you should do is this:

System.out.println("abc\r\ndef".matches(".*abc$\r\n.*")); // false
System.out.println("abc\r\ndef".matches("(?m).*abc$\r\n.*")); // true

I'm not familiar with how community wiki works, but feel free to use this example if deemed useful.

Pattern.matches("(?m)^abc$\n^def$", "abc\ndef")
); // prints "true"

Pattern.matches("(?sm)^abc$.^def$", "abc\ndef")
); // prints "true"

Category:java Views:1 Time:2010-04-25

Related post

  • How to create a java regular expression pattern that would match a string only at certain positon? 2012-02-23

    I would like to create a regular expression pattern that would succeed in matching only if the pattern string not followed by any other string in the test string or input string ! Here is what i tried : Pattern p = Pattern.compile("");//I w

  • skip over HTML tags in Regular Expression patterns 2009-04-09

    I'm trying to write a regular expression pattern (in python) for reformatting these template engine files. Basically the scheme looks like this: [$$price$$] { <h3 class="price"> $12.99 </h3> } I'm trying to make it remove any extra tabs\s

  • How can I match everything with a PHP regular expression? 2009-06-02

    How can I match everything with a PHP regular expression? I tried: /[.\r\n]*/, but it isn't working. Any ideas? Thanks. This is for a method I made for a PHP class to parse e-mails: public function getHeader($headerName) { preg_match('/[\r\n]' . $hea

  • Pass variable to regular expression pattern string in jquery 2010-06-12

    Is that possible to pass variable into regular expression pattern string in jquery ( or javascript)? For example, I want to validate a zip code input field every time while user type in a character by passing variable i to the regular expression patt

  • Expect (the language) "couldn't compile regular expression pattern: quantifier operand invalid" error 2010-07-28

    I'm having trouble with an Expect regular expression. I'm trying to match on this output: RUC.hg0 : 6 +6 ITPOK.hg0 : 6 +6 ITUC.hg0 : 6 +6 ITPKT.hg0 : 6 +6 IT127.hg0 : 6 +6 ITBYT.hg0 : 456 +456 IR127.hg0 : 6 +6 IRPKT.hg0 : 6 +6 IRUC.hg0 : 6 +6 IRPOK.h

  • Howto make regular expressions pattern for this? 2012-02-15

    I want to make a regular expression pattern for a string like Function_Name ( 'parameters' ) Function Description this line has to be split so I get the name , parameters and description --------------Solutions------------- Assuming parameters are se

  • Pre-built regular expression patterns or Regex Libraries? 2008-08-14

    Does anyone use have a good regex library that they like to use? Most of the regexes that you find online either contain bugs or are so focused on the edge cases that it turns into a competition to validate whatever spec 100%. Of course you can write

  • please can anyone help me to convert this regular expression pattern from javascript to c# 2011-06-15

    please can anyone help me to convert this regular expression pattern from javascript to c# var phonePattern = /^(([^\.\-\,a-wy-z]([\(]?(\+|[x])?\d+[\)]?)?[\s\.\-\,]?([\(]?\d+[\)]?)?[\s\.\-\,]?(\d+[\s\.\-\,]?)+[^\.\-\,a-z])|((\+|[x])?\d+))$/i i need t

  • Regular expression pattern for a string like 12,345,678,900? 2011-07-04

    As you can see from the title, I would like to write a regular expression pattern to find a string that consists of various numbers and is separated by comma every three digits. The length of string can vary. I am still pretty new to regular expressi

  • How do I do this with one regular expression pattern instead of three? 2011-07-04

    I think I need to use an alternation construct but I can't get it to work. How can I get this logic into one regular expression pattern? match = Regex.Match(message2.Body, @"\r\nFrom: .+\(.+\)\r\n"); if (match.Success) match = Regex.Match(message2.Bo

  • is there any way to get all the possible outcomes of a regular expression pattern? 2012-01-27

    is there any way to get all the possible outcomes of a regular expression pattern?. everything I've seen refers to a pattern that is evaluated against a string. but what I need is to have a pattern like this: ^EM1650S(B{1,2}|L{1,2})?$ generate all po

  • Match a Query to a Regular Expression in SQL? 2009-12-03

    I'm trying to find a way to match a query to a regular expression in a database. As far as I can tell (although I'm no expert), while most DBMS like MySQL have a regex option for searching, you can only do something like: Find all rows in Column 1 th

  • How to build a regular expression pattern? 2011-02-07

    When I create postal code regular express pattern which requires 10 alphanumeric of fixed length. For example,canadian postal code format: alpha,numeric,alpha,numeric,alpha,numeric, left-justify and pad with spaces to meet the fixed length. The patte

  • How can I create a regular express pattern which has 30 charater fixed length? 2011-02-08

    How can I create a regular express pattern which has 30 charater fixed length, but the charater in the pattern could be less than 30 and pad with spaces. For example, I want to enter these kind of values: Jack Howe Katherine Joe Ken's Workstation The

  • Matching numbers with decimals in regular expression 2011-07-02

    I want to see if a number that I have is between two other numbers. Let's say my number is 0.17. I would like the regular expression to be something like this: [0.15-0.22]. Could someone help get me on the right track? --------------Solutions--------

  • Need Regular Expression pattern for validating path directory structure in Flex? 2011-08-10

    I need a regular expression pattern for validating the following path directory: "C:/SomeDefaultPath/Input/swift/*.pdf" and "../Input/swift/*.pdf" I've tried the regexp pattern got from net search, but I couldn't validate the above mentioned paths. A

  • Setting XML attribute regular expression pattern in JAXB? 2011-10-25

    Is there a way to specify a regular expression pattern on an @XmlAttribute somehow through annotations? Is there a way of doing this besides validation through an external schema? --------------Solutions------------- You could use an implementation o

  • Regular Expressions pattern with Special characters 2011-12-06

    I'm working on a regular expressions pattern, but it contains a number of special characters. I'm not really sure how to incorporate them in a normal regex pattern string. Specifically, I need to test to see if a string contains '+/-'... I've tried u

  • Simple regular expression pattern for php 2012-02-16

    How to write a regular expression that will pass a numbers from 60 to 99? --------------Solutions------------- [6-9][0-9] Fairly straightforward: /^[6-9][0-9]$/ But why are you using regex for this? Regular expressions is a poor tool for such task. Y

Copyright (C), All Rights Reserved.

processed in 0.152 (s). 11 q(s)