r/ProgrammerHumor • • May 07 '21

irregex

Post image
8.3k Upvotes

153 comments sorted by

716

u/Vardy May 07 '21

After so many years of doing regex, I still can't tell if thats valid or not.

730

u/tomthecool May 07 '21
$n}i++{<c"¿e[\69]^

Yes it is, but it will never match anything.

$ means "end of line", so it cannot possibly be followed by an n. But reading on anyway...

  • } is just a literal character.

  • i++ is one-or-more i character (a possessive quantifier, i.e. does not allow any back-tracking, although this doesn't actually make any difference here -- so it's basically the same thing as writing i+).

  • {<c"¿e are again just literal characters.

  • [\69] is a character group of either the octal character U+0006 (which is actually an ACK control character) or the number 9.

  • ^ means "start of line" which, again, cannot possibly match in this context.

328

u/cuplizian May 07 '21

is it possible to learn this power?

105

u/wanz0 May 07 '21

Not from a Jedi

43

u/Ravens_Quote May 07 '21

Did you ever hear the tragedy of Darth Cii the Sharp?

29

u/gothicVI May 07 '21

20

u/cuplizian May 07 '21

this is actually a very good tool for beginners. I personally started to learn regex from https://regexr.com since (for me at least) it's easier to learn there. but eventually I switched to regex101 for regular use

→ More replies (1)

3

u/ste_3d_ven May 07 '21

There is always https://regex101.com/ which is probably the closet a mortal can come to learning the powers of the gods.

5

u/golgol12 May 07 '21

A better question is "Should you learn this power?"

For then you'll always have two problems instead of one.

→ More replies (1)
→ More replies (1)

47

u/Kanthes May 07 '21

{ and } can be used as quantifiers when used as a pair, n{3,5}, so I'd be wary of that messing stuff up. Ideally you'd want to escape them with a backslash if you wanted to capture the literal character.

33

u/tomthecool May 07 '21

Yes, that's true, but I was just describing how the above would be parsed.

Ignoring the obvious absurdity of putting a $ at the start of the pattern, and a ^ at the end of the pattern, and the overall complexity of this mess, here's how I would opt to write it:

$n\}i+\{<c"¿e(\x06|9)^

23

u/Kanthes May 07 '21

Honestly I think we're just both addicted to trying to understand any regexp we see like they're some sort of puzzle.

46

u/tomthecool May 07 '21

I wrote this library to generate strings that match an arbitrary regex several years ago, purely for the fun/challenge of figuring it all out from scratch.

→ More replies (2)
→ More replies (1)
→ More replies (3)

9

u/gastonci May 07 '21

🤔what if its a multi line regex?

10

u/tomthecool May 07 '21

No difference.

Depending on the regex flavour (programming language) and flags (multi-line), ^/$ might either mean "start/end of string" or "start/end of line". But in this case, it's irrelevant. "End of line/string" can never be immediately followed by an n character.

If the regex looked more like this:

$\n}......

...then your question would be more valid.

→ More replies (2)

4

u/LinAGKar May 07 '21

I had the same thought, but the problem is that ^ and $ don't consume any characters. They match 0 characters after or before a newline, but not the newline itself.

7

u/[deleted] May 07 '21 edited May 07 '21

can we talk about

([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\ [0-9][ABD-HJLNP-UW-Z]{2}|(GIR\ 0AA)|(SAN\ TA1)|(BFPO\ (C\/O\ )?[0-9]{1,4})|((ASCN|BBND|[BFS]IQQ|PCRN|STHL|TDCU|TKCA)\ 1ZZ))

or

[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]

The second should be obvious, bonus points if you get the first one. I love a good riddle. If you use google you fail.

17

u/tomthecool May 07 '21 edited May 07 '21

The first one is probably a British postcode regex?

And the second one is a poor man's email regex, which is clearly not RFC-compliant, but is also the sort of thing millions of developers copy+paste off stackoverflow to use on their websites.

6

u/[deleted] May 07 '21

Respect. You got them both and yes the second is a poor mans email regex I made many years before stack overflow even existed. Didn't use them on a website just in excel. Who knew you could use regex in excel of all things? I just pulled them from that file just for fun.

2

u/KriegerClone02 May 07 '21

Except you can use multi-line regex, which could include $ and ^ in places other than the end and start off the pattern respectively. Usually this would only work with something like "$\R", but it is actually possible to redefine the end-of-line sequence in some parsers.
The "{" is more problematic, but even that depends on which variant of regex you are using.

→ More replies (3)

1

u/MrSteamie May 07 '21

Question, could you apply this to right-to-left script that was handled improperly, ie it doesn't properly use the command characters to switch to "true" right-to-left typing?

3

u/tomthecool May 07 '21

I vaguely understand your question, but this doesn't exactly make sense to me. What exactly is a "RTL script, handled improperly, not using control characters"? :D

If I literally write the regex backwards:

^]96/[e¿"c<{++i}n$

...then this is now invalid, because there's an unclosed character group.

But if I also flip those brackets around:

^[96/]e¿"c<{++i}n$

...then yes, this is now a valid regexp, and a string like 9e¿"c<{{{i}n matches it.

→ More replies (5)
→ More replies (17)

2

u/numerousblocks May 07 '21

Parse error because of }, I think.

2

u/finitogreedo May 07 '21

Nope. Because they are allowed to be independent. That's just a literally bracket character. Though personally, I'd put a backslash in front to clarify it...

Doesn't mean there are parse issues elsewhere. But that isn't technically one.

→ More replies (4)

75

u/RDfromMtHare May 07 '21

Quick! Seal the facility! Cut all network connections!

5

u/PranshuKhandal May 07 '21

search and delete all occurences

6

u/[deleted] May 07 '21

But how can we detect all occurences?

8

u/PranshuKhandal May 07 '21

we need to make a regular expression for searching irregular expression

4

u/[deleted] May 07 '21

As long as irregular expressions aren't anything like HTML, count me in.

→ More replies (2)

36

u/jfb1337 May 07 '21

The "regexes" offered in most programming languages are already irregular.

11

u/[deleted] May 07 '21

concatenation, alternation, repetition is all we should need, but i'd ask for sets and negated sets so we can deal with unicode without going mad

2

u/Kered13 May 08 '21

Sets and negated sets are both forms of alternations, so they are fine. Forward references, back references, and look aheads are what make a grammar irregular.

→ More replies (1)
→ More replies (2)

23

u/halfphysicshalfmath May 07 '21

Waiting for someone to link an obligatory xkcd comic on regex

18

u/Cpt_Daniel_J_Tequill May 07 '21

oof imagine that

58

u/lady_Kamba May 07 '21

The next step up from regular expressions would be context free expressions.

chomsky hierarchy

43

u/tomthecool May 07 '21

Technically the things we refer to as "regex" are context-free expressions.

For example: Look-aheads, back-references and subexpression calls are not, in terms of the chomsky hierarchy, "regular".

10

u/lady_Kamba May 07 '21

This makes sense, had I thought about it instead of assuming that regular expression had regular grammar, I might have come to the same conclusion.

→ More replies (1)

1

u/Kered13 May 08 '21

You should avoid using those if you can though, as they cause the runtime to be exponential instead of linear.

→ More replies (2)

16

u/[deleted] May 07 '21

Sometimes I think I'm smart, and sometimes I try to understand a Wikipedia page about chomsky hierarchy.

12

u/[deleted] May 07 '21

It's not about being smart, you probably just need previous knowledge, like already understanding Chomsky hierarchy before reading the Wikipedia. Most of the text there is only reference, not very good introductions.

2

u/[deleted] May 07 '21

I'm glad to hear that, because that page made 0 sense to me.

4

u/dadbot_3000 May 07 '21

Hi glad to hear that, I'm Dad! :)

4

u/[deleted] May 07 '21

Alan Turing would be proud...

1

u/[deleted] May 08 '21

wait, anarchist grandpa contributed to the CS world too? holy cow

1

u/Kered13 May 08 '21

He actually started as a linguist. He created the Chomsky Hierarchy as part of his work on formal grammars, in an attempt to find a universal grammar for human language. This work found new application in computer science when people started writing parsers for programming languages.

7

u/Yuca965 May 07 '21

¡¿ ǝuop noʎ ǝʌɐɥ ʇɐɥʍ

5

u/[deleted] May 07 '21

Shit, who leaked my bank password? Damn Russians.

3

u/phpdevster May 07 '21

regular expressions vs this

*insert Pam Beesly "they're the same picture" meme*

3

u/[deleted] May 07 '21

Since regex is gibberish, shouldn't the meme have a readble word?

5

u/HanlonsDullBlade May 07 '21

Perfectly regular if you convert the string to spanish and parse it as like Hebrew....

2

u/plcolin May 07 '21

Wouldn’t that be a Backus−Naur form?

1

u/jlamothe May 07 '21

Should be between backslashes.

1

u/[deleted] May 07 '21

i wish Kleene could write an essay about this

1

u/vo9do9 May 07 '21

Skience

1

u/thepromaper May 07 '21

Can someone explain what the fuck is that?

1

u/thepromaper May 07 '21

Can someone explain what the fuck is that?

1

u/mardabx May 07 '21

Sounds like javascript without extra steps.

1

u/hk2k1 May 07 '21

Guys im having a hard time learning regex .. is there any website to learn from ?

2

u/[deleted] May 07 '21

Idk about specific websites with best info on it, but I highly recommend a live parser for it like https://regex101.com/

It'll allow you to experiment and get a grasp on things without having to like recompile or contrive a test case over and over in your application.

1

u/notmondayagain May 07 '21

as if normal regular expressions are not complex already!

1

u/[deleted] May 07 '21

Is this random keyboard bashing or actual regex LOL

1

u/[deleted] May 07 '21

69, nice

1

u/NightshadeLotus May 07 '21

People can just use regex101 dot com

1

u/cbusalex May 07 '21

But can you parse HTML with it?

1

u/Wipit11 May 08 '21

Looks to me like it says snitch 69 but that’s just my simple brain

1

u/Generic_Reddit_Bot May 08 '21

69? Nice.

I am a bot lol.

1

u/[deleted] May 09 '21

There should be a regex like system for context free grammars.