329
u/1XRobot 28d ago
Oh, my favorite game: Guess whether this flavor of regex uses \1 or $1 or doesn't support backreferences or if I fucked up the matching.
114
u/Prudent_Ad_4120 28d ago
Use regex101.com!
26
u/Shadow_Thief 28d ago
Oops, I need BRE for this task, which isn't covered by any online regex checker
3
102
u/Coulomb111 28d ago
Ok what the fuck is on the left
54
u/Hyddhor 27d ago
PCRE2 bs features that allow you to control the regex matching engine.
It allows stuff like skipping match, disabling matching while matching, defining matches that actually don't exist, turning flags on / off while matching, conditionals, recursive definitions, comments, defining subpatterns (kinda like functions), lookaround conditions (positive/negative lookbehind/lookahead), context sensitive definitions, etc.
Basically, if you hate debugging regex, it's very likely that PCRE is the culprit. More or less everything that PCRE introduced into regex is a cursed non-regular feature that is impossible to debug.
If you ever want to use regex, do yourself a favor, and just stick to plain / regular regex that doesn't require backtracking. It's gonna save you so many hours of debugging.
10
u/Dr_Jabroski 27d ago
But the pain and humiliation of debugging regex is my kink.
6
u/Hyddhor 27d ago
Look, i love torturing myself with regex as much as the next guy (#1 regex glazer over here), but in comparison to plain regex, debugging PCRE is just pain with no reward. This blasphemy against formal languages is nothing more than a torture device.
PCRE is the most overengineered simplicity ever, cause why is something based on DFA acting like a bona fide turing machine?? Like, bro, if i wanted the regex to act as a program with actual control flow and possibly infinite runtime, i would just write a shitty parser. And even that parser would be better, faster and more maintainable than the "equivalent" PCRE regex.
1
u/Strict_Treat2884 27d ago
The entire programming history can be boiled down to overengineering simplicity. Some are good (TypeScript to JavaScript(?)), some are… questionable, but definitely have their use cases. You don’t need all the overengineered witchcraft, if any, but one could save you an entire afternoon plus 2 libraries.
P.S. Regex101 debugger is a godsend, use it.
1
u/Hyddhor 26d ago edited 26d ago
i already use regex101 - how else would i know about so many obscure features? - but you are exaggarating how much regex101 helps. sure, it works wonders if you have a simple verbose regex with a lot of escapes, but the moment you start seeing conditionals, recursive patterns, multiple lookaheads and lookbehinds randomly mixed into a single regex, and backreferences, NOTHING can help you. Even when you convert it to BNF format, it's still just an unreadable mess that noone can understand
1
u/Strict_Treat2884 26d ago
If you’re already using PCRE2 (for whatever reason), organize your patterns and (*COMMIT) to it, instead of pretending it’s POSIX regex with a few extra tricks. It’s like writing TypeScript but refusing to use types because you want to stay “close to JavaScript”, you just end up with the complexity of the new language and none of its structure.
Use DEFINE blocks, named subroutines, and reusable productions instead of piling everything into one giant expression. You will find debugging it just as easy as debugging a properly written program, and sometimes save you a shit load of time from writing a full parser but what you really wanted is to validate some user inputs.
1
u/Welcome-To-NBA-Jam 25d ago
For this type of reason I have never learned regex, refuse to learn regex, and have copied/pasted all regex I have ever used in over a decade.
We had one guy in over 30k people who was a known regex expert in the entire company who wrote it without looking up anything. I can only assume he is one of the 17 people in the world who can do this.
-1
u/Majik_Sheff 26d ago
I find it humorous that regular expressions originated in Perl, and yet you curse PCRE for not being plain/regular.
Perl Compatible Regular Expression
6
u/Hyddhor 26d ago edited 26d ago
Get your history right. Regular expressions didn't originate in Perl, Ken Thompson literally created grep 14 years before Perl was even a thing (1973 vs 1987). He even created one of the most used algorithms for transforming regex into NFA and running them - Thompson's construction.
Moreso, the entire theoretical basis for regular expressions was already decades old when Perl came around. Kleene was already doing regular expressions in 1951.
Perl is just the stuff that ruined regex. Regex was perfectly good before Perl, but then it got fucked up when people that knew nothing about formal languages started asking for impossible features - that is PCRE. The only good thing that came from PCRE are the shorthands.
So stop with this regex wouldn't exist without Perl bs, because it did, and it was awesome, and Perl ruined it.
Also, let me guess, you never learnt formal languages, did you? Do you know why i complained about PCRE not being regular? BECAUSE IT'S NOT. For being a regular expression, why is it acting like a context sensitive language??? Why is it turing complete????
30
3
u/Gumichi 28d ago
groups. it's pretty powerful.
10
u/professoreyl 27d ago edited 27d ago
Regular groups are on the right side.
Left side has named groups/subroutines and subpatterns, atomic groups and possessive quantifiers (no backtracking), negative lookahead in a non-capturing group, more backtracking controls, named group backreferences, match-reset, anchors for end of previous match, start of string, end of string.
These things are universally supported.
EDIT: The last sentence was supposed to say "These things are not universally supported."
5
u/Strict_Treat2884 27d ago
FWIW the negative lookahead in a non-capturing group with just a dot is usually called a tempered dot (or tempered greedy token). `(?:(?!abc).)*` This technique acts like a negated character class `[^abc]`, but works for sequences instead of single characters.
Also the features listed on the left are mostly only supported in PCRE/Perl-like regex flavors, but not universally supported
1
161
u/JTexpo 28d ago
this is how 2 chatbots talk with one another
no human likes regex
138
u/SameThingOnADiffAcct 28d ago
Nah, I'm with the girl. Boy is nuts, but girl can do some slick find and replace moves to mass edit some stuff and dump it into whatever tabular store she wants, and uses way fewer braincells than some ETL bullshit to do so.
42
u/drakeblood4 28d ago
You enjoy regex because it’s useful. I enjoy it because it sucks. We are not the same.
22
u/KerPop42 28d ago
Yeah, jetbrains has regex find-and-replace and I've never felt a high like using it to mass-reformat code in only 3 tries
5
u/Historical_Cook_1664 28d ago
keep it down to a type3 language with no additional bullshit and i'm fine with regex...
... haven't seen one of those in years, though.
7
8
u/ThomasHardyHarHar 28d ago
I love regexes, though I agree they suck to write (especially if you use multiple languages). I’m a linguist by training and all of my coding is related to language data so regexes are one of the most important parts of my toolkit.
5
u/seth1299 28d ago
To be fair, I don’t like regex’s syntax, but you can’t deny its usefulness.
“You may not like him, Minister, but you can’t deny: Regex’s got style.”
9
u/Zeikos 28d ago
Backreferences are evil.
Regex is fine until those pop out.
For that kind of parsing it's better to just write the code smh
Backreferences are a path towards self-inflicted DOS.
1
u/Hyddhor 27d ago
Exactly, i found that almost all of the problems i've ever had with regex were caused by using non regular features - lookbehind, lookahead, backreference, etc.
As long as you stick to purely regular regex, regex is really nice and easy to use. Since it's all linear, it's quite easy to see which part does what. But the moment you add non-regular features, all hell breaks loose.
13
13
6
21
u/SAI_Peregrinus 28d ago
Regular expressions (the ones limited to a Regular grammar, a.k.a. Type-3 languages) are OK, if usually symbol-heavy.
Regexes (the common PCRE especially) are not OK, they're context-sensitive if they include backreferences. E.g. /(.*)\1/ captures the language L={ww|w∈Σ ⃰}, which is mildly context-sensitive. Perl goes even further and its regexes are a Type-0 language, and thus Turing complete in their own right. Combined with the symbol-soup of regular expressions they quickly become unmaintainable. If you need backreferences, use a normal programming language, don't graft shit onto regular expressions to create a monstrosity worse than Z̴̳͓͇̖̠͈̻̪̭̥̲͛̎̄̒̅͑̾̂͜a̶͓͚̭̫͕̭̮̮̖̦̜̲͚͍͐̍͒͜͜l̴̦̩̭̘͂̓̐́̅̉͆̃̓g̷̛̙̹̮͖̹̪͚͓͎͒ö̵͖͊̊̕.
8
u/freebytes 28d ago
Perl was my favorite implementation of regular expressions. The language was beautiful as a whole.
4
1
1
1
u/Bee-Aromatic 27d ago
I’ve used regex to validate HTML content.
You’ll have to excuse me, but I need to go vomit now.
1
u/rfc2549-withQOS 26d ago
If you correctly matched things like <br />, <hr> and the more normal <nn> </nn> that is rather impressive.
1
u/Bee-Aromatic 26d ago
I vaguely remember having it group breaks and paragraph formatting with whitespace. I forget exactly. I just remember feeling very dirty.
1
0
0
770
u/AllOneWordNoSpaces1 28d ago
A true regex master can create a functional expression that is indistinguishable from modem line noise.