The first one doesn't: \w can match digits, at least in most regex engines I've used.
It's usually described as matching "word" characters, which includes digits and _s, since those're (usually) allowed in variable/function/class names (that is: the kind of words programmers are often most interested in).
OK, so now someone needs to work out the proper regular expression for it :) Like 1) contains L, G, B, T in any order, 2) contains any other letters after the main four, 3) ends in plus
4! works out to 24 permutations. At that point it would be easier and more readable to just write a regular function:
def test(inp):
if not inp.endswith('+'):
return False
if len(inp) < 6:
return False
if not all((i in inp[:4] for i in 'LGBT')):
return False
if any((i not in string.ascii_uppercase for i in inp[4:-1])):
return False
return True
But then that breaks for the versions that intentionally do repeat letters, like with repeated Qs for both "queer" and "questioning".
Almost fixed by replacing the middle . in the lookahead with (another) [LGBT], unless one of those 4 letters are repeated later, but I can't recall ever seeing that.
EDIT: just saw your other comment. Not sure if I like that method better or worse than the one I suggested here...
Allows any letter or number after the first 4. I couldn't figure out how to limit a lookahead to just 4 characters, so I had to enumerate the possible places of repetition. Numbers allowed after first 4 for 2S.
At the start of the string, look forward and reject any of the following:
the first character repeats in any of the next 3 positions
the second character repeats in any of the next 2 positions
the third character repeats in the 4th position
If the lookahead didn't reject, match on any character in LGBT exactly 4 times.
Then, match on all capitalized alphanumerics any number of times.
Although thinking about it, it's possible to interpret the original request as "starts with any positive number of Ls, Gs, Bs, and Ts, followed by any number of alphabetical characters that aren't L, G, B, or T, then ends in a +. But I felt like doing that would've been malicious compliance!
Really wish modern regular expressions would follow syntax closer to what I keep seeing in theory of computation courses. In other words, change that . to a Σ.
one-more-re-nightmare used to let you write Σ, but I then tried to search Greek stuff with it and it went wrong. So now there's...$ for all characters (since that's not used for end-of-line assertions).
23
u/[deleted] Jun 10 '22
My gender is the " . "
40
u/uwu-dotcom Jun 10 '22 edited Jun 10 '22
I'm smart enough to know this is a regex joke, but too stupid to understand it.
17
u/kiyyik Jun 09 '22
OK, so now someone needs to work out the proper regular expression for it :) Like 1) contains L, G, B, T in any order, 2) contains any other letters after the main four, 3) ends in plus
5
u/emipyon Jun 10 '22
LGBTQQQQ
3
u/_Second_2_2 Jun 10 '22
Lol
3
u/thatlightningjack Jun 10 '22
[LGBTQ]+(.*)
I'm BTA (bi+trans+aro?)
3
u/k819799amvrhtcom Jun 22 '23
import LGBTQ.*;
2
u/[deleted] Jun 10 '22
not gonna lie, took me reading it twice :D
1
u/kotrenn Jun 10 '22
Really wish modern regular expressions would follow syntax closer to what I keep seeing in theory of computation courses. In other words, change that . to a Σ.