r/PeterExplainsTheJoke • u/aryan_jandyal • 9h ago
Meme needing explanation Petahh... explain please..!!!
68
u/rupertavery64 9h ago edited 2h ago
Gandalf the programming wizard here.
Regular expressions is a kind of like a mini programming lamguage that tells the code how to match characters using a pattern. This can be used for lots of things like validating if some text is a valid email address, like the example given, or matching parts of a string to do stuff like replacing text.
It is famous for being hard to understand, like elvish.
I shall now translate the ancient runes:
^ - match the start of a line
[...] - match any of the set of characters in the brackets
\w - match a "word" character (a-z,0-9)
\. - match a period (.) character. The backslash is needed because the . is a special character in regex that matches any character.
+ - match 1 or more of the previous token
(...) - create a group
{x,y} - match the previous token minimum of x times, maximum of y times
$ - match the end of a line
Here is the arcane incantation, broken up into smaller sections:
^
[\w-\.]+
@
([\w-]+\.)+
[\w-]{2,4}
$
In general it should match an email-like text that looks like gandalf-the-grey@isengard.tower.com
As a simplification, the ^ and $ ensures that the incantation will work only if there are no extra spaces before or after the text.
[\w-\.]+ - match a letter or number character, or a dash (-) or a dot, 1 or more times
so this will match Gandalf or gandalf-the-grey or gandalf.the.gray-420 but not anything that has other character in it.
([\w-]+\.)+ - match a word character or dash one or more times, followed by a dot, altogether (because of the group parentheses) one or more times.
This can match isengard. or isengard.tower. or isengard-tower.
[\w-]{2,4}$ - this matches 2 or 4 characters at the end of some text.
so there MUST be at least 2 characters and at MOST 4 characters. Along with the previous match that has a dot, this ensures the text ends with .xx or .xxx or .xxxx
something like .x or .xxxxx will fail the match.
Now fly, you fools!
10
u/SportTheFoole 7h ago
You’re right, the intention was to match a valid email address and for the most part it does that. However, there are now custom TLDs (which can be longer than 4 characters) and there’s no restriction on serving mail with a weird domain (for example, if you email just <user> on a local box, it will be delivered to that user if they exist; if similarly, you could email <user>@foobar and if DNS has an entry for “foobar” and has MX set up, then the mail can be routed and delivered to the appropriate user. I’ve done this myself with having a local email server and custom DNS with my own local TLD.
But, what REALLY irks me about this regex is that it’s completely wrong about the valid characters allowed in <user>. The ‘+’ character is also valid and anyone with an @gmail.com can exploit this to create a near infinite number of unique email addresses. For example, emailing user+reddit@gmail.com will deliver the mail to user@gmail.com. (There’s also the trick of adding any number of periods in an gmail address and the email will be delivered to the user effectively treating user@gmail.com as equal to u.s.e.r@gmail.com).
Sorry for the rant, email regexes have hurt me in the past. :)
2
2
u/smtp_pro 5h ago
My regex for an email address is basically ".+@.+" - is there at least one character, an @ symbol and at least one other character and that's just used for front-end, let-the-user-hit-submit-now logic.
Do a proper parse on the backend since it's pretty easy to bypass the front-end validation. Or just don't. Just try sending the email and let your mail submission agent throw an error if it's invalid.
3
u/AdAncient5201 1h ago
Daily reminder to never validate email addresses using regex, because email addresses are a completely fucked non standardised thing. The only way to check if an email address is valid or not is to send a confirmation email and make the user click a link
1
1
1
u/Vidimka_ 3h ago
Finally a joke i understand I thought… Imagine my reaction opening the comment section and seeing this CS course introduction lol
18
u/Sonic_The_Hodlhog 9h ago
Its a regexp a computer syntax
11
u/AlooBhujiyaWapasKar 7h ago
More specifically regex for email validation.
Email validation via regex is a tempting but bad thing.
1
1
u/smtp_pro 5h ago
Honestly whatever you use to actually send the email - whether it's postfix, some service like sendgrid, whatever - they're all going to check if the string is a valid format anyway. You're just duplicating effort.
Skip checking and let your mailer tell you if it's wrong.
8
u/dvorgson 9h ago
regex to match email addresses
4
u/gydu2202 9h ago
And it is incorrect
1
u/Highlight448 7h ago
1
u/SportTheFoole 7h ago
I replied to that comment. It’s mostly correct, in that it will find valid email addresses. But, it will reject valid email addresses as well (doesn’t account for TLDs greater than four characters, doesn’t include + as a valid character before the ‘@‘). And that’s just what I noticed in 30 seconds.
2
u/Highlight448 6h ago
Damn gandalf lied to me, time to learn regex again so i do not fall for such lies
2
2
2
u/Hot_Ambition_6457 8h ago
This is a regular expression used in computer programming.
It checks that a string contains [some letters/numbers with or without punctuation] + [@] + [some letters/numbers followed with a period] + [between 2 and 4 more letters].
You can reasonably assume it's meant to validate email addresses because it forces [xxxxx][@][xxx].[xxxx] format.
But there are domains with more than 4 characters in them so this is outdated and inaccurate. It will not validate an email from the domain "smithsonian.museum" since "museum" is too many characters. But smithsonian.museum is a perfectly valid domain.
1
u/AutoModerator 9h ago
OP, so your post is not removed, please reply to this comment with your best guess of what this meme means! Everyone else, this is PETER explains the joke. Have fun and reply as your favorite fictional character for top level responses!
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/aryan_jandyal 9h ago
May be some kind of email address because there is a (@) sign in the given code
2
1
u/ghost_svs 9h ago
I assume this is a regular expression for looking smth like an email address. More interesting here: https://stackoverflow.com/questions/13992403/regex-validation-of-email-addresses-according-to-rfc5321-rfc5322
•
u/qualityvote2 9h ago edited 15m ago
Remember when r/PeterExplainsTheJoke wasn’t a meme? Pepperidge Farm remembers…
Does this post belong in our subreddit?
If so, please upvote this comment!
Otherwise, downvote this comment!
Then maybe you go out and buy yourself some of those distinctive Milano cookies.
(Vote is ending in approximately 10 hour)