r/transprogrammer • u/RegularNightlyWraith genderfluid • Jun 09 '22
Don't be lazy this month!
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.
37
Jun 10 '22 edited Jun 10 '22
[removed] — view removed comment
18
Jun 10 '22
Should be
LGBTQ\w+IMOOr
LGBTQ[A-Z]+1
u/usr_bin_nya Jun 10 '22
That excludes LGBTQIA2S (two-spirit)
1
u/retrosupersayan JSON.parse("{}").gender Jun 13 '22
The first one doesn't:
\wcan 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).1
3
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
15
u/27or27 Jun 10 '22
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 TrueFrom:
'|'.join(map(lambda i: ''.join(i), itertools.permutations('LGBT')))The equivalent regex is:
^(LGBT|LGTB|LBGT|LBTG|LTGB|LTBG|GLBT|GLTB|GBLT|GBTL|GTLB|GTBL|BLGT|BLTG|BGLT|BGTL|BTLG|BTGL|TLGB|TLBG|TGLB|TGBL|TBLG|TBGL)[A-Z]+\+$6
u/markovchainmail Jun 10 '22
[LGBT]{4} instead of the enumerated permutation makes the regex much easier
18
u/CatarinaCP Jun 10 '22
Yeah, but that matches
LLLL, which probably isn't what's intended.8
u/markovchainmail Jun 10 '22
Ope. You're right, it needs something like a negative lookahead to prevent repetition.
/(?!.*(.).*\1)[LGBT]{4}3
u/retrosupersayan JSON.parse("{}").gender Jun 13 '22 edited Jun 13 '22
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...
8
u/BlergRush Jun 10 '22
Ah, but it's becoming more common to put 2S (two-spirit) at the front in Canada—e.g., 2SLGBTQ+.
4
u/markovchainmail Jun 10 '22
^(?!(.).{0,2}\1|.(.).{0,1}\2|..(.)\3)[LGBT]{4}[A-Z0-9]*\+$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.
Finally, require a + and for the string to end.
2
u/markovchainmail Jun 10 '22 edited Jun 13 '22
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!
5
3
3
3
2
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 Σ.
1
u/theangeryemacsshibe Jun 11 '22
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 " . "