Live data from Hacker News

Regex character "$" doesn't mean "end-of-string"

sethmlarson.dev

61–70 of 382 posts

Re: Regex character "$" doesn't mean "end-of-string"

#61
post #58
post #27

I can hear thousands of bad hiring manager's adding 'How do you match the end of a string in a regex?' to their list of 'Ha! You don't know the trick!' questions designed to catch out candidates.

"I will hire you anyway, but I will pay you less" Regex, useful in any job...

regex is useful but chatgpt is amazing at it, so why spend a minute keeping such useless knowledge in mind.

if you know where to find something no point in knowing it.

Re: Regex character "$" doesn't mean "end-of-string"

#62
post #21

Special misery case: Visual Studio supports regex search, where '$' matches \n. The end of line character is usually the standard Windows \r\n. Yes, that means if you want to really match the end of line you have to match "\r$". So broken.

FWIW, and I know this doesn't really address your complaint: I use Windows and I've set all my text editors to use LF exclusively years ago and Things Are Great. No more weird Git autocrlf warnings, no quirks when copying files over to/from people on Macs or Linuxes, etc. Even Notepad supports LF line endings for quite a long time now - to my practical experience, there's little remaining in Windows that makes CRLF "…

I could never get visual studio (not code) to not use \r\n when editing a solution file via the gui

Re: Regex character "$" doesn't mean "end-of-string"

#64
post #55

Earlier quoted context omitted.

Same, tho it'd be interesting to see if this behavior holds if the file ends without a trailing newline and your match is on the final newline-less line.

Fortunately, it's pretty simple to test. $ printf 'Line with EOL\nLine without EOL' | grep 'EOL$' Line with EOL Line without EOL $ grep --version | head -n1 grep (GNU grep) 3.8

The line does end with the file, so it's logically consistent.

It's not matching the newline character after all.

Re: Regex character "$" doesn't mean "end-of-string"

#65

> Folks who've worked with regular expressions before might know about ^ meaning "start-of-string" and correspondingly see $ as "end-of-string". Huh. I always think of them as "start-of-line" and "end-of-line". I mean, a lot of the time when I'm working with regexes, I'm working with text a line at a time so the effect is the same, but that doesn't change how I think of those operators. Maybe because a fair amount of…

It's kind of driving me nuts that the article says ^ is "start of string" when it's actually "start of line", just like $ is "end of line". \A is apparently "start of string" like \Z is "end of string".

Re: Regex character "$" doesn't mean "end-of-string"

#66
post #63

Isn't a string with a newline character automatically multiline? The new line is just empty but not the first line anymore.

No, it is not.

    3.195 Incomplete Line

    A sequence of one or more non- characters at the end of the file.

    3.206 Line

    A sequence of zero or more non- characters plus a terminating  character.
courtesy of [0]. See also [1] for rationale on "text file":

   Text File

   [...] The definition of "text file" has caused controversy. The only difference between text and binary files is that text files have lines of less than {LINE_MAX} bytes, with no NUL characters, each terminated by a . The definition allows a file with a single , or a totally empty file, to be called a text file. If a file ends with an incomplete line it is not strictly a text file by this definition. [...]
[0] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1...

[1] https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd...

Re: Regex character "$" doesn't mean "end-of-string"

#67
post #52

> Note: The table of data was gathered from regex101.com, I didn't test using the actual runtimes. Has anyone confirmed this behaviour directly against the runtimes/languages? Newlines at the end of a string are certainly something that could get lost in transit inside an online service involving multiple runtimes.

> Newlines at the end of a string are certainly something that could get lost in transit inside an online service involving multiple runtimes. In what way could newlines at the end of a string "could get lost in transit"?

If you write it to a text file by itself and then read it from that text file, each runtime can have a different definition of whether a newline at the end of the file is meaningful or not. Under POSIX, a newline should always be present at the end of a non-empty text file and is not meaningful; not everyone agrees or is aware.

There are plenty of other ways, too; bugs happen.

Re: Regex character "$" doesn't mean "end-of-string"

#68
Raku (formerly Perl 6) has picked ^ and $ for start-of-string and end-of-string, and has introduced ^^ and $$ for start-of-line and end-of-line. No multi line mode is available or necessary. (There's also \h for horizontal and \v for vertical whitespace)

That's one of the benefits of a complete rethink/rewrite, you can learn from the fact that the old behavior surprised people.

Re: Regex character "$" doesn't mean "end-of-string"

#70
post #12

Does anyone consider RegEx to be standardised? Moving to a new context is always a relearning exercise in my experience.

The ISO/IEC 14882 C++ standard library mandates [0] implementations for six de jure standard regex grammars: IEEE Std 1003.1-2008 (POSIX) [1] BRE, ERE, awk, grep, and egrep and ECMA-262 EcmaScript 3 [2].

So, yes, at least someone (me) considers regex to be standardized in several published de jure standards.

  [0] https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf#chapter.28
  [1] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html
  [2] https://262.ecma-international.org/14.0/#sec-regexp-regular-expression-objects
Post reply on HN