pronouns, regex, cursed 

After seeing this St. Hey post I got intrigued to write a regular expression that would capture he, she and they but nothing else.

Alas, I'm not a regex pro, so my if-then-else statements don't seem to work. Could you tell me what's wrong here? Or is it just that bash can't handle this shit?

^(t)?(?(1)|s?)he(?(1)y|)$

pronouns, regex, cursed 

@Stoori I would write /\b(s?he|they)\b/ (in Perl syntax) but if you want to use a conditional expression then I'd write

/\b(s?(t)?he(?(2)y))\b/

\b is a zero-width word boundary, this will work on a word in a sentence, not just a word on its own.

Your main mistake is that every () captures and so gets a number, start from the leftmost and outermost, so

(t) => 1
(?(1)|s?)he(?(1)y|) => 2

So the condition should have (2) and I don't think you need a condition for the s.

Follow

pronouns, regex, cursed 

@wim_v12e I'm trying to do this on bash, which for some reason requires the | in the if statement, otherwise it returns nothing.

Anyway, after adding |, it captures the following:

he
she
the

So for some reason the if statement for y doesn't work on my setup.

(And yes, it would be easiest to just capture literals he/she/they, but the whole point of this excercise is to make this as awkward and impractical as possible. :D)

Perhaps I should just give up! :D

pronouns, regex, cursed 

@Stoori I've never used bash regexes, sorry.

Sign in to participate in the conversation
Polyglot City

Polyglot City is the right instance for you, if you're interested in languages, language learning and translating, or if you are multilingual or polyglot. All languages are allowed to flourish on our timelines. Welcome!