r/badcode • • Oct 21 '20

python Ever heard of \w?

Post image
1.3k Upvotes

58 comments sorted by

76

u/GandelXIV Oct 21 '20

This regex checks if the username contains only alphanumeric characters and checks that its length is 5-12 characters.

8

u/survivalking4 Oct 21 '20

This might have been programmatically generated. I mean I hope it is for humanity's sake

3

u/GandelXIV Oct 21 '20

No it wasnt.

10

u/survivalking4 Oct 21 '20

People really worry about AI taking over the world and then do this

2

u/fun_egg Oct 22 '20

May be AI should take over, so we don't have to see code like this.

399

u/n3wsw3 Oct 21 '20

This is why people think they are afraid of regex. This monstrosity can be boiled down to "\w{5,12}"

10

u/evolseven Oct 21 '20

Yah, I love regex.. even back-references aren’t that hard once you get a clear grasp on it. Even reading it is not terribly difficult once you get it.. but, Perl is also my preferred language so I may just be a masochist that likes magic..

17

u/robin_888 Oct 21 '20

4

u/Ali3nat0r Oct 22 '20

There go 2 hours of my life while I learn and practice regex. Sad thing is, I'll probably forget most of it by next week...

1

u/toepicksaremyfriend Oct 22 '20

You’re not alone. I’m right there with you!

0

u/[deleted] Oct 21 '20

[deleted]

215

u/crunchyintheory BuddyPress Oct 21 '20

How about [a-zA-Z0-9]{5,12}?

10

u/danbulant Oct 21 '20

I actually prefer this over \w - it's descriptive enough so even those who have just basic understanding yet don't know what some shortcuts are can see what it does just by looking at it

5

u/new2bay Oct 21 '20

Bingo. The OP looks like it was written by someone traumatized by a DFA in college.

7

u/ywBBxNqW Oct 21 '20

That's probably just fine if you assume that the only allowed characters are those in that code snippet. It doesn't take Unicode into account at all though but maybe that's fine for OP.

1

u/[deleted] Feb 02 '21

How would you recommend learning / memorizing regex? It seems really useful, I just don’t know where to start

1

u/crunchyintheory BuddyPress Feb 02 '21

Hm, I'd try to develop an application with some practical applications for regex. I first learned it when developing a URL-detection algorithm as a component of a larger project, and then I added to that knowledge every time I ran into a pattern-matching problem.

1

u/[deleted] Feb 02 '21

I can do that. I’m trying to make a thing that converts from discord’s markdown to Minecraft’s JSON RAW format, so that seems like an excellent use for regex...

0

u/[deleted] Oct 21 '20

[deleted]

53

u/Farsqueaker Oct 21 '20

Not portable that way, though.

27

u/OnlySeesLastSentence Oct 22 '20

If you can't fit like 20 characters on a drive, you need a bigger drive.

8

u/crunchyintheory BuddyPress Oct 21 '20

TIL, does that work outside of pcre?

33

u/ywBBxNqW Oct 21 '20

It's entirely dependent on the implementation of the regex utility/library you use.

80

u/Beanalby Oct 21 '20

I think this entire thread has shown *THIS* is why people are afraid of regex.

32

u/mfb- Oct 21 '20

All the standard things are independent of the implementation (excluding some obscure corner cases).

Something that doesn't understand [a-zA-Z0-9]{5,12} is not worth using.

15

u/xigoi Oct 21 '20

However, even \w is implementation-dependent (sometimes it matches non-ASCII letters, sometimes not).

4

u/EncapsulatedPickle Oct 22 '20

Same with \d, which I learned the hard way.

2

u/RFC793 Oct 22 '20

Sometimes you don’t get to choose

3

u/ywBBxNqW Oct 21 '20

I think the entirety of these comments show that people hold mixed views about regular expressions for whatever reasons.

2

u/binarycat64 Oct 28 '20

It's [[:alnum:]]

16

u/Zentrosis Oct 21 '20

Regex is just like any other code. Complicated if you don't understand it, or if it's too long 🤷🏼‍♂️

8

u/ywBBxNqW Oct 21 '20

You could argue that there are a lot of things that seem complicated if you don't understand them. :)

0

u/[deleted] Oct 21 '20

[deleted]

1

u/jarfil Oct 21 '20 edited Dec 02 '23

CENSORED

50

u/veryusedrname Oct 21 '20

Where is our image translating volunteer?

83

u/Zelphy712 Oct 21 '20 edited Oct 21 '20

verify_username_regex = """ ((a|b|c|d|e|f|g|h|I|j|k|l|m|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9) (a|b|c|d|e|f|g|h|I|j|k|l|m|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9) (a|b|c|d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|g|h|I|j|k|l|m|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9) ?(a|b|c|d|e|f|g|h|I|j|k|l|m|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9)) """

hi I'm a volunteer only for this post, I don't super know how reddit formatting works and Im on mobile so if it's bad I'll figure out how to fix it. enjoy!

18

u/veryusedrname Oct 21 '20

You are a crazy maniac, I hope you will never get angry with me. :D

19

u/StrangePractice Oct 21 '20

I don’t even think the most passionate of volunteers would give this awful code the time of day to give it new life here

13

u/SarcasticCodeBot Oct 21 '20
from string import ascii_lowercase, ascii_uppercase
print('verify_username_regex = \"\"\"\n('+'\n'.join([b[0] if a<5 else '?'+b[0] for a,b in enumerate(['('+'|'.join(list(ascii_lowercase)+list(ascii_uppercase)+[str(c) for c in list(range(10))])+')'] for b in range(12))])+')\"\"\"')

0

u/[deleted] Oct 21 '20

[deleted]

5

u/redditsoaddicting Oct 21 '20

f'...'

Am I remebering that right? I know Python has them.

1

u/Kurinoku Oct 22 '20

you mean like raw strings? or string interpolation?

1

u/redditsoaddicting Oct 22 '20

Raw strings, but now that you mention it, f was probably interpolation.

1

u/Kurinoku Oct 22 '20

yeah i checked, r"" is raw, f"" is interpolated

1

u/LonelyContext Oct 22 '20

What's wrong with that?

0

u/[deleted] Oct 22 '20

[deleted]

1

u/LonelyContext Oct 22 '20

Yeah I mean I guess that’s what happens when code is written by a Bot that also claims to be Sarcastic.

32

u/JohnMcPineapple code! Oct 21 '20 edited Oct 08 '24

...

21

u/causticacrostic Oct 21 '20

when you only read the first paragraph of the documentation

13

u/Ilix Oct 21 '20

I like my RegEx like I like my women, verbose.

9

u/[deleted] Oct 21 '20

My rule is: if it's tedious you're doing it wrong.

14

u/mandarBadve Oct 21 '20

OMG I am forgetting Reg-Ex now by looking at this

4

u/Dreadedsemi Oct 21 '20

At least they know their abc.

3

u/maedox Oct 21 '20

This can't be real!

3

u/youdrumyouvomit Oct 22 '20

jesus who has the time for that

2

u/A1337Xyz Oct 21 '20

When you are trying to code without internet.

2

u/NOMASAN163 Oct 22 '20

Haha.... I remember my 9 hours of work for a WORKING editor program... with unique and correct spacing between each and every letter

9 hours of work, 947 lines of code.

2

u/Hozerino Oct 22 '20

brilliant

2

u/KingOfKingOfKings Dec 23 '20

'Explicit is better than implicit'.

0

u/[deleted] Oct 21 '20

[deleted]

14

u/Wiwwil Oct 21 '20

Yeah, but from the "art" you need to replace the "a" by "p", "r" by "o", and "t" by "o"

8

u/ThaiJohnnyDepp Oct 21 '20

echo art | tr rat opo

2

u/[deleted] Oct 21 '20

Art with an sh