You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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);
}
@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.
@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.)
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.
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.
Activity
BurntSushi commentedon Jun 7, 2016
Could you please provide a test case that doesn't act as you expect?
jminer commentedon Jun 7, 2016
Sure, here is a program that I would expect to print "Matched: true", but it prints "Matched: false":
BurntSushi commentedon Jun 7, 2016
That is indeed expected behavior.
$will only match\nin multi line mode. It's not clear to me whether supporting\r\nis feasible unfortunately.BatmanAoD commentedon Mar 21, 2017
@BurntSushi Would treating
\ras an end-of-line character, and\nas 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\ris 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.BurntSushi commentedon Mar 21, 2017
@BatmanAoD That sounds feasible from an implementation perspective, but I'm not a fan of implementing something that's wrong. (Essentially no systems use
\rfor line endings any more, and on Windows, it's\r\n, not\r.)BatmanAoD commentedon Mar 21, 2017
But the existing implementation is more wrong, so I'm not sure I understand that as an objection.
BatmanAoD commentedon Mar 21, 2017
(Also, just last week I actually did run into something that uses
\ron its own as EOL by default: Putty in serial mode! I was shocked.)mitsuhiko commentedon Mar 22, 2017
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.
BurntSushi commentedon Mar 22, 2017
BurntSushi commentedon Mar 22, 2017
I'm going to re-open this, but I don't have any immediate plans to work on it.
mitsuhiko commentedon Mar 22, 2017
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.
mitsuhiko commentedon Mar 22, 2017
So the regex behavior here does match other engines dispite what I said earlier.
BurntSushi commentedon Mar 22, 2017
@mitsuhiko Oh interesting. I should have known for Go, but it's interesting to see that Python doesn't do it either:
So I guess we're in good company?
63 remaining items
syntax: add support for CRLF-aware line anchors
syntax: add support for CRLF-aware line anchors
syntax: add support for CRLF-aware line anchors
syntax: add support for CRLF-aware line anchors
syntax: add support for CRLF-aware line anchors
syntax: add support for CRLF-aware line anchors
syntax: add support for CRLF-aware line anchors
syntax: add support for CRLF-aware line anchors
syntax: add support for CRLF-aware line anchors
syntax: add support for CRLF-aware line anchors
syntax: add support for CRLF-aware line anchors
changelog: 1.9.0
Update Rust crate regex to 1.9.1 (#1957)
changelog: 1.9.0