News for hackers. Stuff that matters.
Re:Regex character "$" doesn't mean "end-of-string" by amelius on Wednesday March 20, 2024 @10:33AM (#39767169)
What is driving me nuts is that we have Unicode now, so there is no need to use common characters like $ or ^ to denote special regex state transitions.
Re:Regex character "$" doesn't mean "end-of-string" by yjftsjthsd-h on Wednesday March 20, 2024 @10:39AM (#39767261)
If we were willing to ignore the ability to actually type it, you don't need Unicode for that; ASCII has a whole block of control characters at the beginning; I think ASCII 25 ("End of medium") works here.
Re:Regex character "$" doesn't mean "end-of-string" by knome on Wednesday March 20, 2024 @10:35AM (#39767206)
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.
Re:Regex character "$" doesn't mean "end-of-string" by keybored on Wednesday March 20, 2024 @12:12PM (#39768567)
It’s not that silly. You constantly get into escape conundrums because you need to use a metacharacter which is also a metacharacter three levels deep in some embedding.

(But that might not solve that problem? Maybe the problem is mostly about using same-character delimiters for strings.)

And I guess that’s why Perl is so flexible with regards to delimiters and such.

Re:Regex character "$" doesn't mean "end-of-string" by LK5ZJwMwgBbHuVI on Wednesday March 20, 2024 @01:42PM (#39769798)
Yes, languages really need some sort of "raw string" feature like Python (or make regex literals their own syntax like Perl does). That's the solution here, not using weird characters...
Re:Regex character "$" doesn't mean "end-of-string" by keybored on Thursday March 21, 2024 @04:24AM (#39775991)
Fine enough. But I wonder why strings have to use the same delimiter. Imagine if you had a list delimiter `|` and the answer to nested lists was “ohh, use raw list syntax, just make `###||` when you are three levels deep or something”.
Re:Regex character "$" doesn't mean "end-of-string" by keybored on Thursday March 21, 2024 @09:53AM (#39778513)
On the third hand strings are of course a special case because you always have special characters and whatnot which makes raw strings useful. :) Not just doing `"` and stuff.
Re:Regex character "$" doesn't mean "end-of-string" by Karellen on Friday March 22, 2024 @10:57AM (#39791358)
It is quite nice what `sed` does. A sed search-and-replace is typically shown as `s/foo/bar/`, but you can actually use any punctuation character to separate the parts. Whatever follows the "s" will be used for that statement, so you can write `s|foo|bar|` or `s:foo:bar:`, even mixing and matching in the same script to have `s|baz|quux|; s:xyzzy:blorp:` and it will all work.
Re:Regex character "$" doesn't mean "end-of-string" by pepa65 on Friday March 29, 2024 @03:02AM (#39861377)
The weird characters are part of the syntax here. Of course, you can make it more verbose, or more flexible/configurable.
Re:Regex character "$" doesn't mean "end-of-string" by FranOntanaya on Wednesday March 20, 2024 @11:20AM (#39767863)
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.
Re:Regex character "$" doesn't mean "end-of-string" by cortesoft on Wednesday March 20, 2024 @12:04PM (#39768440)
I write regexes all the time, and I don't know if I would be CHALLENGED by that, but it would be annoying. Escaping things is trivial, and since you do it all the time it is not anything extra to learn. Having to remember bespoke keystrokes for each character is a lot more to learn.
Re:Regex character "$" doesn't mean "end-of-string" by keybored on Wednesday March 20, 2024 @03:06PM (#39770697)
ASCII restriction begets ASCII toothpick soup. Either lift that restriction or use balanced delimiters for strings in ASCII like backtick and single quote.

(“But backtick is annoying to type” said the Europeans.)

Re:Regex character "$" doesn't mean "end-of-string" by int_19h on Wednesday March 20, 2024 @06:19PM (#39772677)
Regexes are one case where I think it's already extremely unbalanced wrt being easy to write but hard to read. Using stuff like special Unicode chars for this would make them harder to write but easier to read, which sounds like a fair deal to me. In general, I'd say that regexes should take time and effort to write, just because it's oh-so-easy to write something that kinda sorta works but has massive footguns.

I would also imagine that, if this became the norm, IDEs would quickly standardize around common notation - probably actually based on existing regex symbols and escapes - to quickly input that, similar to TeX-like notation for inputting math. So if you're inside a regex literal, you'd type, say, \A, and the editor itself would automatically replace it with the Unicode sigil for beginning-of-string.

Re:Regex character "$" doesn't mean "end-of-string" by emporas on Wednesday March 20, 2024 @11:43PM (#39774719)
Regexes originate from Perl, or they were popularized by Perl if i got this right. In Perl readable code is not ranked as one of it's top 100 priorities. Regexes could originate from J and situation could be even worse though!
Re:Regex character "$" doesn't mean "end-of-string" by TristanBall on Thursday March 21, 2024 @04:31AM (#39776038)
Regex's predate perl quite substantially. Think grep and friends if nothing else.

Certainly making the perlre library available separate to perl encouraged its widespread use, and lots of others copied or were inspired by it.

"Popularized" doesn't seem like quite the right word though, I don't disagree with the point, but if I shout "Hey everyone let's write regex's" at the office people throw stationary at me, which is not true of other popular things!

Re:Regex character "$" doesn't mean "end-of-string" by wlonkly on Thursday March 21, 2024 @03:44PM (#39783462)
Perl spread regexes.
Re:Regex character "$" doesn't mean "end-of-string" by emporas on Thursday March 21, 2024 @04:53PM (#39784265)
Super spreader event we've got ourselves into.

I took a look at Raku, which claims be a better Perl maybe, or closely related but more modern, it certainly looks nice. Although i am a big fan of typed languages, Raku piqued my interest.

Re:Regex character "$" doesn't mean "end-of-string" by lizmat on Friday March 22, 2024 @04:51AM (#39788639)
In Raku, if you want to go typed, you can! It's called "gradually typed".

my Int $a = 42; # ok

my Int $a = "foo"; # Type check failed in assignment to $a; expected Int but got Str ("foo")

Re:Regex character "$" doesn't mean "end-of-string" by emporas on Friday March 22, 2024 @09:07AM (#39790329)
Very nice, good to know. Yes i know gradual typing, Python has a form of that i think. I will check out Raku at some point, the type system will not go unnoticed. I didn't even know it had one!
Re:Regex character "$" doesn't mean "end-of-string" by amelius on Wednesday March 20, 2024 @11:33AM (#39768039)
Also, code is read more often than it is written.
Re:Regex character "$" doesn't mean "end-of-string" by cortesoft on Wednesday March 20, 2024 @12:05PM (#39768457)
People say this all the time, but is it really always true? I have a ton of code that I wrote, that just works, and I never really look at it again, at least not with the level of inspection that requires parsing the regex in my head.
Re:Regex character "$" doesn't mean "end-of-string" by wccrawford on Thursday March 21, 2024 @09:21AM (#39778205)
Even for code I wrote once and then never have to fix, I end up reading it multiple times while I create it and the lines around it. I think it really is always true.
Re:Regex character "$" doesn't mean "end-of-string" by Yujf on Wednesday March 20, 2024 @10:38AM (#39767233)
Why not? Common characters are easier to type and presumbly if you are using regex on a unicode string they might include these special characters anyway so what have you gained?
Re:Regex character "$" doesn't mean "end-of-string" by amelius on Wednesday March 20, 2024 @11:55AM (#39768340)
In theory yes, in practice no.

What you have gained is that the regex is now much easier to read.

Re:Regex character "$" doesn't mean "end-of-string" by knome on Wednesday March 20, 2024 @12:35PM (#39768920)
It's easy to read now.
Re:Regex character "$" doesn't mean "end-of-string" by LK5ZJwMwgBbHuVI on Wednesday March 20, 2024 @01:44PM (#39769828)
> In theory yes, in practice no.

That's like "in theory we need 4 bytes to represent Unicode, but in practice 3 bytes is fine" (glances at universally-maligned utf8mb3)

Re:Regex character "$" doesn't mean "end-of-string" by int_19h on Wednesday March 20, 2024 @06:28PM (#39772773)
It's not really an issue if the string you're matching might have those characters. It's an issue if the regex you are matching that string might need to match those characters verbatim. Which is actually pretty common with ()[]$ when you're matching phone numbers, prices etc - so you end up having to escape a lot, and regex is less readable especially if it also has to use those same characters as regex operators. On the other hand, it would be very uncommon to want to literally match, say, ⦑⦒ or ⟦⟧.
Re:Regex character "$" doesn't mean "end-of-string" by codethatwerks on Wednesday March 20, 2024 @06:57PM (#39773029)
The problem with using an eggplant to denote end of string is backwards compatibility.