5 ms·
tl;dr: Use \A and \z rather than ^ and $ in regexps, because the latter two match newlines in the middle of strings, whereas the former strictly only match the
by cduan 14y ago
tl;dr: Use \A and \z rather than ^ and $ in regexps, because the latter two match newlines in the middle of strings, whereas the former strictly only match the beginning and end of strings.
I thought this was common knowledge, but it's a good reminder for anyone who doesn't know.
- darklajid 14y agoI have no ruby knowledge of any kind. Do you mean this is common knowledge in the relevant field? The regular expressions I _am_ familiar with would work as expected and usually need a special modifier/flag to match over multiple lines. So - I learned something new.
- homakov 14y agoNO. Every f-king book teaches us to use $^, every f-king article on the web. Stop breaking standards, we use it because we used too. It has never been a common knowledge. Ask all rubyists your know and you will see it.
- getsat 14y agoThis is absolutely not common knowledge. I have never seen an applicant use \A or \Z instead of ^/$. They typically also use and/or instead of &&/|| and potentially introduce subtle bugs into their code when the former would suffice.
- getsat 14y agoEr, the latter! You almost never need to use and/or compared to how frequently you use &&/||. Sorry for the mistake.
- masterleep 14y agoI've never heard of this before. Not only is this surprising, the fix is ugly.
- homakov 14y agoCaptain Obvious: All who says "I knew it" are just wanna look smarter than others. It's OK but it doesn't mean that others knew it.
- eevee 14y agoA subtle problem with this "common knowledge": \z is different across languages. In PCRE (Perl and Ruby), \z matches the end of a string; \Z matches the end of a string but allows for a single trailing newline. In Python, \Z matches the end of a string. There is no \z. In POSIX extended regexes, there is no \z or \Z. I think. Who knows, man.