Live data from Hacker News

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

sethmlarson.dev

211–220 of 382 posts

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

#211

In Lua it's only the start/end of the string > A pattern is a sequence of pattern items. A caret '^' at the beginning of a pattern anchors the match at the beginning of the subject string. A '$' at the end of a pattern anchors the match at the end of the subject string. At other positions, '^' and '$' have no special meaning and represent themselves. https://www.lua.org/manual/5.3/manual.html#6.4.1 Lua's pattern matc…

> In Lua it's only the start/end of the string

There's an additional caveat: if you use the optional "init" parameter to specify an offset into the string to start matching, the ^ anchor will match at that offset, which may or may not be what you expect.

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

#212

Earlier quoted context omitted.

How did you internalize it? Perl looks like cat keyboarding.

The same way people internalize punching data and instructions into stacks of cards, or internalize advanced mathematical notation. Just because things aren't written in plain english words doesn't mean they can't be internalized.

Advanced math is mostly written in plain English, actually!

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

#214
post #196

Earlier quoted context omitted.

the idea of changing a decades old convention to instead use, as I assume you are implying, some character that requires special entry, is beyond silly.

I don't think anyone that writes regex would feel specially challenged by using the Alt+ | Ctrl+Shift+u key combos for unicode entry. Having to escape less things in a pattern would be nice.

Also, code is read more often than it is written.

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

#216
post #157

Earlier quoted context omitted.

Yes, that is a file with zero lines that ends with an "incomplete line". Processing of such files by standard line-oriented utilities is undefined in the opengroup spec. So, for instance, the effect of "grep"ping such a file is not defined. Heck, even "cat"ting such a file gives non-ideal results, such as colliding with the regular shell prompt. For this reason, a lot of software projects I work on check and correct…

> Yes, that is a file with zero lines that ends with an "incomplete line". It's a file with zero complete lines. But it has 1 line, that's incomplete, right? The file starts empty. Anything in it starts "a line". So it's 1 incomplete line. I hate weird states.

It's a file with 0 lines and some trailing garbage.

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

#217
post #140

Earlier quoted context omitted.

The line delimiter is a newline. If you have a file containing `A\nB\nC` in a file, the file is three lines long. I guess it could be argued that a file containing `A\nB\nC\n` has four lines, with the fourth having zero length. That a regex is applying to an in memory string vs a file doesn't feel to me like it should have different semantics. Digging into the history a little, it looks like regexes were popularized…

Technically the “newline” character is actually a line _terminator_. Hence “A\n” is one line, not two. The “\n” is always at the end of a line by definition.

Technically, that is one of two possible interpretations, and you seem to have invented a "by definition" out of thin air.

Very very technically a "newline" character indicates the start of a new line, which is why it is not called the "end-of-line" character.

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

#219
post #2

this is mostly due to the different types of regex and less about it being platform dependent. $ was end of string in pcre which is the "old" perl compatible regex. python has its own which has quirks as mentioned, re2 is another option in go for example, and i think rust has its own version as well iirc.

"$" could be end of string or end of line in perl, depending on the setting (are you treating data as a multiline text, or each line separately). (/m, /s,...)

Yeah I accidentally said string when I absolutely meant to say line there.

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

#220
post #157

Earlier quoted context omitted.

So if you have "A" in a file with no newline, there are no lines in that file?

Yes, that is a file with zero lines that ends with an "incomplete line". Processing of such files by standard line-oriented utilities is undefined in the opengroup spec. So, for instance, the effect of "grep"ping such a file is not defined. Heck, even "cat"ting such a file gives non-ideal results, such as colliding with the regular shell prompt. For this reason, a lot of software projects I work on check and correct…

The opengroup spec says no such thing.
Post reply on HN