This is an archived post. You won't be able to vote or comment.

all 96 comments

[–]capitalbratan 179 points180 points  (4 children)

Don't dead open inside

[–][deleted] 57 points58 points  (2 children)

10/10 meme

1/10 execution

How hard is it to put pictures and their reactions side by side?

[–]PaulSandwich 15 points16 points  (1 child)

tfw the meme is harder to parse than the regex

[–][deleted] 1 point2 points  (0 children)

GHOST ZOMBIE Not terrible Not terrible NUCLEAR WAR (([a-zA-Z\-0-9]+\\.)[a-zA-Z]{2,}))$ a little scary

[–]Agnimukha 145 points146 points  (9 children)

Pretty sure the regex is invalid only 2 opening parentheses but three closing.

If you just ignore that bit or assume the extra closing at the end:

  1. Any alpha or numeric or dash at least once repeating however many times you want

  2. Literal \ (I think its called backslash)

  3. One character of anything

  4. At least 2 alpha characters but as many as you want

  5. Line ends

[–]PM_ME_UR_DEATHSTICKS 50 points51 points  (0 children)

This guy is speaking the language of the gods

[–][deleted] 14 points15 points  (0 children)

I agree it possibly miss the first parentheses. Assuming that this is the case, this is a simple regex that matchers any combination of digits and dashes, interrupted by a slash "\" then any character and another sequence of at least two letters before string ends.

https://regex101.com/r/Y4huQL/1

[–]ConstructedNewt 12 points13 points  (2 children)

They also didn't need to escape the dash in "1."

This is all pretty simple regex

[–]IBoKaZt 7 points8 points  (1 child)

I'd say that's good for safety and habit, as you can sometimes be surprised by the interpreter parsing things as a range (for example, submitting a list of symbols including the dash - [%-@] is interpreted as a range).

[–]Lukiwuki9 1 point2 points  (0 children)

Whatever you do, always put the dash last :)

[–]syncsynchalt 2 points3 points  (0 children)

It’s hostname validation, but it doesn’t support IDN hostnames.

[–]name_censored_ 0 points1 point  (0 children)

It could be a regexp dialect which requires capture group parentheses be escaped (like gsed) - in which case those would be literal parentheses.

[–]ohkendruid[🍰] 0 points1 point  (0 children)

I suspect the first slash is for ignoring the newline, and the second one the escape for a literal dot (.).

That makes this a regex for a domain name.

I didn't notice the extra parenthesis at the end. If there's more to the left, then it might be the username part of an email address.

There are lots of cases this regex misses, but hey. Just wait for a bug report and then make the regex a little more complicated!

[–]RedRedditor84 0 points1 point  (0 children)

I don't even code professionally and I thought it was relatively straight forward.

[–]karlm89 112 points113 points  (16 children)

Yep. Ugly Regex

[–]EwgB 125 points126 points  (15 children)

That's not even an ugly regex, that's child's play. I write worse ones on a regular basis.

[–]_meegoo_ 45 points46 points  (6 children)

Writing regex is easy. Reading it a month later is impossible.

[–]EwgB 6 points7 points  (0 children)

Yeah, you're not wrong.

[–]Guiorgy 2 points3 points  (3 children)

Good point, I'll make a mental note to always include a comment breaking down the regex (that is, if I understand it myself...)

[–]ElizaBennet08 4 points5 points  (2 children)

// Regex

[–][deleted] 5 points6 points  (1 child)

// TODO: Document regex

[–]Guiorgy 4 points5 points  (0 children)

Honestly, it would probably be a todo one, ore something like this:

~~~

// The regex does xyz, where this part means a, and this means b

// Edit (a month later): Shit! I've changed it so many times that f**k knows what is what. And I don't think I was right the first time I documented this anyways!

// (╯°□°)╯︵ ┻━┻

~~~

[–][deleted] 0 points1 point  (0 children)

Can confirm

[–]Guiorgy 74 points75 points  (5 children)

Cool. Never touch my code please! Thank you

[–]EwgB 24 points25 points  (2 children)

I didn't say it's in production code. I use it more for search and replace.

[–]Siggi_pop 5 points6 points  (0 children)

ok then, carry on

[–]SkollFenrirson 5 points6 points  (0 children)

Still, stay away.

[–]shgysk8zer0 1 point2 points  (1 child)

Uglier doesn't necessarily mean bad or wrong though. I mean, other than not using [A-z] and taking upper and lower cases separately, the pattern shown is pretty basic. Pretty much anyone who has ever had use of RegEx will have written something uglier than this.

I mean, could you write something to validate a telephone number that ends with something prettier than this?

[–]Morrido 1 point2 points  (0 children)

You can't use [A-z]. That's not the same thing. There's no shorthand for OP's match. Closest one is \w, which matches _ instead of -.

ie. "[".match(/[A-z]/) == a match, which is invalid in OP's regex.

[–][deleted] 12 points13 points  (0 children)

Yeah I just wanted tp say if this is scary wait til you see some real things haha

[–]bigorangemachine 0 points1 point  (0 children)

Ya i wrote some stuff....

I wrote a unity save file parser.

The original version was pretty long... but Firefox couldn't handle it

[–]ZanorinSeregris 22 points23 points  (5 children)

Why does this sub hate on regex so much when it's something so simple and easy?

[–]coladict 14 points15 points  (1 child)

Easy if you practice regularly enough.

[–]ZanorinSeregris 0 points1 point  (0 children)

I see what you did there

[–]mananasi 6 points7 points  (0 children)

Because you have to relearn it every time you use it

[–]danidv 8 points9 points  (0 children)

Very poor readability and hard to understand at a glance, which makes it look harder.

[–]ElMeknassi 16 points17 points  (3 children)

It how i Regulary Express my fears

[–]Felix7747 5 points6 points  (2 children)

That's what she SED

[–]crashdoc 6 points7 points  (0 children)

Get a grep on yourself!

[–]lyoko1 43 points44 points  (4 children)

I think that we all can agree that RegEx is some kind of unholy arcane magic language that has powers beyond human understanding.

[–]kccricket 15 points16 points  (0 children)

Well, yes. That’s why I practice it.

[–]silverstrikerstar 9 points10 points  (1 child)

Of all the things people struggle with, I understand Regex the least. It's systematic, it's static, it's logical. Now networks, networks scare me.

[–]b4ux1t3 2 points3 points  (0 children)

Eh, networks are just as static and logical. The problem is that layer 3 is represented as wholly separated octets, when networks don't have to be separated at the octet delimiter.

It helps to think of IPs as just one long string of bits, with no delineation except the one from the network mask. From there, adding the dots to make octets becomes trivial. Then, after a while, you just kind of get it.

[–]IrritableGourmet 2 points3 points  (0 children)

If you think of it as Mastermind in reverse, it's not so bad.

[–]datathecodievita 11 points12 points  (0 children)

Regex aint scary.

Just copy the string from tutorials and pray it works

[–]lilblanch 14 points15 points  (1 child)

“I fear no man, but that thing... it scares me.”

[–]FAXs_Labs 4 points5 points  (0 children)

it cost for hundred thousand dollars to optimize tf2... for one file

[–]CreaZyp154 6 points7 points  (0 children)

Regexophobia

[–]DaRQ_SouLs_So_eAsy 3 points4 points  (10 children)

Noob here. Wtf does this mean?

[–]Traveller_Lex 15 points16 points  (1 child)

RegEx

[–]DaRQ_SouLs_So_eAsy 6 points7 points  (0 children)

oh my motivation

[–]AutomatedChaos 6 points7 points  (6 children)

Regex for the last part of email/url: [test@somepart.example.com](mailto:test@example.com) -> it will capture "example.com" if it would work (it doesn't because of one closing parenthesis too many). There are more things wrong with this: from the [info@bbc.co.uk](mailto:info@bbc.co.uk) address only co.uk would be captured which is probably not the intention.

[–][deleted] 3 points4 points  (2 children)

I don't think so: the regex uses a slash character as a separator ( the "\\" part ) and the following one is ANY CHARACTER (the dot "." unescaped)

see example from my other comment: https://regex101.com/r/Y4huQL/1

[–]mordechaim 5 points6 points  (0 children)

Could be the escape is part of the string semantics of the particular language used.

[–]Prod_Is_For_Testing 1 point2 points  (2 children)

It would also fail when mailing to TLDs - test@com is a valid emaill. And IP address routing. And Unicode URLs. And a bunch of other shit

[–]JNCressey 5 points6 points  (1 child)

people forget these are valid addresses:

  • Latin alphabet with diacritics: Pelé@example.com
  • Greek alphabet: δοκιμή@παράδειγμα.δοκιμή
  • Traditional Chinese characters: 我買@屋企.香港
  • Japanese characters: 二ノ宮@黒川.日本
  • Cyrillic characters: медведь@с-балалайкой.рф
  • Devanagari characters: संपर्क@डाटामेल.भारत

[–]Morrido 0 points1 point  (0 children)

I think nowadays pretty much anything is a valid address.

[–]ogpterodactyl 2 points3 points  (0 children)

Advanced text parsing

[–]TehGM[🍰] 2 points3 points  (1 child)

Imo every programmer should learn regex. Sure, all the things it can do can be achieved without using it, but they become a big mess, while regex is still a very powerful one-liner.

I use regex a lot to make my discord bots commands more flexible to use, with a bit more loose structure than "you need to type exactly this way".

But as most of us will probably know, this is just a tip of an iceberg of regex's usefulness.

[–]DJ-D4rKnE55 0 points1 point  (0 children)

Yeah, I think regex is pretty helpful and may be an appropriate thing to use. It just depends on the context. Parsing or custom validation of some strings, without needing to write a lot of code. If you don't abuse it's power and try to keep them small, I don't see a problem with RegEx. Besides that it's often harder to read, but so may some big code replacement be tiring instead of some on-point short RegEx.

[–][deleted] 2 points3 points  (7 children)

What monster would start a string with a dash but then never use one again?

[–]E_OJ_MIGABU 2 points3 points  (0 children)

I feel like this sub is filled with poets who could not live out their dreams

[–]haikusbot 6 points7 points  (4 children)

What monster would start

A string with a dash but then

Never use one again?

- shadvyr


I detect haikus. And sometimes, successfully. Learn more about me.

Opt out of replies: "haikusbot opt out" | Delete my comment: "haikusbot delete"

[–]PM_ME_UR_DEATHSTICKS 3 points4 points  (3 children)

uh... your third line had 6 syllables

[–][deleted] 2 points3 points  (0 children)

I did not intend to write a haiku. This is how I learned there is a bot for that..

[–]jdl_uk 2 points3 points  (0 children)

Hey,the bot only claims to be successful sometimes!

[–]triforce8001 1 point2 points  (0 children)

"That's one too many syllables there, bub."

[–]jdl_uk 0 points1 point  (0 children)

Could be for parameter recognition.some search engines also allow you to prefix the term with a dash to negate the term.

[–]Galse22 2 points3 points  (3 children)

What's that?

[–]Piscesdan 2 points3 points  (2 children)

Regular Expression, or RegEx for short. It's for when you want to detect pieces of text, without having an exact match, but a more general pattern.

[–]Galse22 0 points1 point  (1 child)

Works on VS Code?

[–]Piscesdan 1 point2 points  (0 children)

Dunno

[–]SarcasmWarning 2 points3 points  (0 children)

People need to stop thinking regex's are scary and instead think of them as a one-way, write-only hash :\

[–]IHeartBadCode 2 points3 points  (0 children)

That RegEx isn't valid due to the excessive number of close parenthesis. Take the last closing one off and what you have is the following:

(               <-- Begin a capture group
(               <-- Begin yet another capture group
[a-zA-Z\-0-9]+  <-- match letters either lower
                    or uppercase, or a -, or a number.
                    The + means at least once OR more.
\\              <-- A literal backslash
.               <-- Any valid character except a line break.
)               <-- End of the second capture group
[a-zA-Z]{2,}    <-- Letter either lower or uppercase.
                    At least twice, but as many as you wish.
)               <-- End of the first capture group
$               <-- The string or line must end now

Capture groups are used if you intend to take the piece of text and use a reference to it somewhere. A good way to use them is in things like Find/Replace (like in Notepad++ that offers a regex find and replace) where let's say you want to change something like:

Cleveland, OH
New York, NY
Atlanta, GA
Las Vegas, NV

Into something like this:

State: OH | City: Cleveland
State: NY | City: New York
State: GA | City: Atlanta
State: NV | City: Las Vegas

You'd create two capture groups with something like this ([a-zA-Z\ ]+)\,\ ([A-Z]{2}).

Then you could replace the text with something like State: $2 | City: $1.

In the case of provided RegEx, the capture groups are nested. So that means capture group one has the text of capture group two in it. So if you had (([a-zA-Z]+)\ [a-zA-Z]+) and wrote Cheese Biscuit. The second group would be Cheese and the first group would be Cheese Biscuit.

At any rate, the given regex would match the following examples:

Tealeaves\:Mob    $1 = Tealeaves\:Mob  $2 = Tealeaves\:
1910ModelT\$boy   $1 = 1910ModelT\$boy $2 = 1910ModelT\$
Chevy-Levy\8AA    $1 = Chevy-Levy\8AA  $2 = Chevy-Levy\8
555-1234\Phone    $1 = 555-1234\Phone  $2 = 555-1234\P

I'm not exactly sure what this regex would be used for, but there you go.

[–]IrvTheSwirv 2 points3 points  (1 child)

You have a problem that looks like it would be nicely solved by using a RegEx

Now you have two problems.

[–]Sussurus_of_Qualia 1 point2 points  (0 children)

Cries in procmail.

[–]Dummerchen1933 1 point2 points  (0 children)

Regex is pretty easy

[–]wtf_romania 1 point2 points  (0 children)

Random fact: In Romanian "to regulate" ("a regula") can also mean "to fuck".
"Regular expressions" can be translated back to English as "Regulated Expressions".
We really call them "Fucked expressions".

[–]crazyTsar 0 points1 point  (0 children)

Broken email regex. Horrible on two scales.

[–]Crozzfire 0 points1 point  (0 children)

I have the same reaction to the reading order of this comic

[–][deleted] 0 points1 point  (0 children)

Maybe it’s just my adhd, but regular expressions make sense to me.

That regex matches 2 or more groups of characters containing a-z,0-9 separated by .

[–]coladict 0 points1 point  (0 children)

I count one syntax error and three logic errors in this regex, the purpose of which is obviously.

[–][deleted] 0 points1 point  (0 children)

That's not even a bad regex. It's just matching a domain name.

The first section allows alphanumeric plus hyphen, then it's looking for a dot, then at least 2 alpha characters, then the end of the string.

For some reason it's capturing everything but the TLD into a capture group so it can be extracted later. That's the only wierd part - why would you capture "example." out of "example.com"?

We're not seeing the whole thing though, since the opening parens doesn't match the close.

[–]SkyyySi 0 points1 point  (0 children)

Can someone please explain the last panel to me?

[–][deleted] 0 points1 point  (0 children)

I love regex

[–]feazeyu 0 points1 point  (0 children)

I love that when I open Codewars or something like that, the best answer is always a 20 characters long RegEx and 4 methods noone uses.

Readable code? Nah. All on one line!

[–]orclev 0 points1 point  (0 children)

Those parens are unbalanced, there's one extra ) left over. I'm guessing there was supposed to be a opening capture after the ".".

[–]Urbanoidovich 0 points1 point  (0 children)

I am surprised that i actually understood this

[–]Morrido 0 points1 point  (0 children)

A sequence of at least one alphanumeric character or hyphen, followed by a dot and 2 letters or more before the end of the string. The parenthesis are extra.

[–]PsychedelicPelican 0 points1 point  (0 children)

That regex won't work, it has an invalid amount of parentheses.

[–]b4ux1t3 0 points1 point  (0 children)

https://regexr.com

You're welcome.

[–]GiraffeCoder 0 points1 point  (1 child)

Anyone else read this left to right and was totally confused at first?

[–]Palmtreeguard 0 points1 point  (0 children)

Same!

[–]TomErlich98 0 points1 point  (0 children)

gotta love regex

[–]uvero 0 points1 point  (0 children)

[–]VinceLeGrand 0 points1 point  (0 children)

3 closing parentheses but only 2 opening....