Live data from Hacker News

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

sethmlarson.dev

161–170 of 382 posts

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

#161
If you really want to learn regex, you'll have a hard time piecing it all together via blog posts.

Brad Freidl's Mastering Regular Expressions is a good book to read if you want to stop being surprised/lost.

I'll admit I stopped at the dive into DFA/NFA engine details.

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

#162

Earlier quoted context omitted.

You don't have to be an expert; you should very rarely be using regexes so complex that you can't understand them.

...and if you can understand them then you clearly understand regex enough not to need ChatGPT to write them

I understand assembly too.

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

#163

Earlier quoted context omitted.

This unexpectedly matches: re.match('^bob$', 'bob\n') I didn't want the trailing newline to be included.

But that string does have a new line at the end.

re.match('^bob$', 'bob') → yes

re.match('^bob$', 'bobs') → no

Most people would expect 'bob\n' not to match, because I used '$' and it has an extra character at the end, just like 'bobs'. In Python it does match because '\n' is a special case.

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

#164
post #32

Earlier quoted context omitted.

Languages invented after Perl will generally use some flavor of Perl regex syntax, but there are always some minor differences. The issue of the meaning of `$` and changing it via multi-line mode is usually consistent though.

I like to think of "whatever browsers do in js" as an updated common baseline. Whatever your regex engine does, describe it as a delta to the js precedent. That thing is just so ubiquitous. I do wonder though what's the highest number of different regex syntaxes I've ever encountered (perhaps written?) within a single line: bash, grep and sed are never not in a "hold my beer" mood!

Isn't JavaScripts regex one of the worst modern regex implementations?

They seem to improve. Negative lookbehind isn't missing anymore [1]. But still lack the handy \Q and \E to escape stuff [2].

[1] https://stackoverflow.com/a/3950684

[2] https://stackoverflow.com/q/6318710

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

#165
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…

> 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.

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

#166
post #87

Earlier quoted context omitted.

what does gpt say how we should validate email addresses?

Prompt: 'I'm writing a nodejs javascript application and I need a regex to validate emails in my server. Can you write a regex that will safely and efficiently match emails?' GPT4 / Gemini Advanced / Claude 3 Sonnet GPT4: `const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;` Full answser: https://justpaste.it/cg4cl Gemini Advanced: `const emailRegex = /^[a-zA-Z0-9.!#$%&' +/=?^_`{|}~-]+@[a-zA-Z0-9](?:…

Terrible answers as far as I can tell, especially Chat got would throw out many valid email addresses.

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

#167

> 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…

This must be the "second problem" everyone talks about with regular expressions.

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

#168

Earlier quoted context omitted.

> Because even after disabling multi-line you are still dealing with line-based semantics when you use ^ or $ No, you're not, except for this weird corner case where `$` can match before the last `\n` in a string. It's not just any `\n` that non-multiline `$` can match before. It's when it's the last `\n` in the string. See: >>> re.search('cat$', 'cat\n') >>> re.search('cat$', 'cat\n\n') >>> This is weird behavior. I…

But that is exactly what it means, the end of the line is before the terminating newline or at the end of the string if there is no terminating newline. Both ^ and $ always match at start or end of lines, \A and \Z match at the start or end of the string. The difference between multi-line and not is whether or not internal newlines end and start lines, it does not change the semantics from end of line to end of strin…

> But that is exactly what it means

I think you've kind of missed the point. Sure if `$` in non-multiline mode means "end of line" the behaviour might be reasonable. But the big error is that people DO NOT EXPECT `$` to mean "end of line" in that case. They expect it to mean "end of string". That's clearly the least surprising and most useful behaviour.

The bug is not in how they have implemented "end of line" matching in non-multiline mode. It's that they did it at all.

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

#169
post #97
post #86

Earlier quoted context omitted.

chatgpt-4: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ https://chat.openai.com/share/696f7046-7f43-4331-b12b-538566... chatgpt-3.5: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ https://chat.openai.com/share/aaa09ae8-3fd9-4df7-a417-948436...

…which both excludes addresses allowed by the RFC and includes addresses disallowed by the RFC. (For example, the RFC disallows two consecutive dots in the local-part.)

What is maybe more important to note, it completely disallows the language of some 4/5 of the humanity. And partially disallows some 2/3 of the rest.
Post reply on HN