Skip to content

$ doesn't match CRLF #244

Closed
Closed

Description

@jminer

I created a regex with multi_line set to true, but after debugging why the regex was matching in a unittest but not in a file, I found out that $ isn't matching the end of a line in the file. I'm using Windows so the newlines are \r\n.

Activity

  1. BurntSushi commented on Jun 7, 2016

    @BurntSushi
    Member

    Could you please provide a test case that doesn't act as you expect?

  2. jminer commented on Jun 7, 2016

    @jminer
    Author

    Sure, here is a program that I would expect to print "Matched: true", but it prints "Matched: false":

    extern crate regex;
    
    use regex::RegexBuilder;
    
    fn main() {
        let regex = RegexBuilder::new("^apple$").multi_line(true).compile().unwrap();
        let text = "\r\napple\r\nbanana";
        let mut matched = false;
        for _ in regex.captures_iter(text) {
            matched = true;
        }
        println!("Matched: {}", matched);
    }
    
  3. BurntSushi commented on Jun 7, 2016

    @BurntSushi
    Member

    That is indeed expected behavior. $ will only match \n in multi line mode. It's not clear to me whether supporting \r\n is feasible unfortunately.

  4. BatmanAoD commented on Mar 21, 2017

    @BatmanAoD

    @BurntSushi Would treating \r as an end-of-line character, and \n as non-EOL if preceded by \r, be an acceptable change? This should be doable in a DFA, I think, though it does mean two EOL states. It might be somewhat surprising behavior when \r is embedded in a line, but that seems like a much rarer case than EOL \r, and it's not even clear to me that treating carriage return as EOL is actually wrong.

  5. BurntSushi commented on Mar 21, 2017

    @BurntSushi
    Member

    @BatmanAoD That sounds feasible from an implementation perspective, but I'm not a fan of implementing something that's wrong. (Essentially no systems use \r for line endings any more, and on Windows, it's \r\n, not \r.)

  6. BatmanAoD commented on Mar 21, 2017

    @BatmanAoD

    But the existing implementation is more wrong, so I'm not sure I understand that as an objection.

  7. BatmanAoD commented on Mar 21, 2017

    @BatmanAoD

    (Also, just last week I actually did run into something that uses \r on its own as EOL by default: Putty in serial mode! I was shocked.)

  8. mitsuhiko commented on Mar 22, 2017

    @mitsuhiko

    I just ran into this when trying to use it to parse some data coming in via HTTP. This is incredibly confusing at the very leasts as this is the only regex engine I know with this behavior.

  9. BurntSushi commented on Mar 22, 2017

    @BurntSushi
    Member
  10. BurntSushi commented on Mar 22, 2017

    @BurntSushi
    Member

    I'm going to re-open this, but I don't have any immediate plans to work on it.

  11. mitsuhiko commented on Mar 22, 2017

    @mitsuhiko

    Just wanted to throw in that I was wrong. It's indeed the same behavior in Python and Go as well. I did never notice in the former because a similar code I was working with stripped the EOL chars whereas the API I used in rust only split on \n and left an empty \r hanging around.

    So when parts of the data were recombined into a string later the \r were left in there.

  12. mitsuhiko commented on Mar 22, 2017

    @mitsuhiko

    So the regex behavior here does match other engines dispite what I said earlier.

  13. BurntSushi commented on Mar 22, 2017

    @BurntSushi
    Member

    @mitsuhiko Oh interesting. I should have known for Go, but it's interesting to see that Python doesn't do it either:

    >>> import re
    >>> re.match('foo$', 'foo\n', re.MULTILINE) is not None
    True
    >>> re.match('foo$', 'foo\r\n', re.MULTILINE) is not None
    False
    

    So I guess we're in good company?

  14. 63 remaining items

  15. added a commit that references this issue on Jul 5, 2023
    8513751
  16. added a commit that references this issue on Mar 12, 2024
    aa64e6d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions