r/ProgrammerHumor • • May 07 '21

irregex

Post image
8.3k Upvotes

153 comments sorted by

View all comments

Show parent comments

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

7

u/entropicdrift May 07 '21

I still use regexr for testing any regex that doesn't rely on lookahead

→ More replies (4)

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.

7

u/Milkshakes00 May 07 '21

You fucking madman. This could be useful.

→ 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 (3)
→ More replies (1)
→ More replies (32)