I'm trying to program a vocabulary game.
I am using a regular expression to hide my word, which I have to guess. I'm not comfortable with the syntax used with regular expressions - outwith the simple examples, I get very confused.
Take for example the verb
'to crank (sth) up'
I want to transform that into:
to _ _ _ _ _ (sth) _ _
The programme will feed from a vocabulary CSV file. My convention is to add (sth)
or (smb)
for transitive verbs. I don't want to hide those bits between brackets. Likewise, I don't want to hide the to
that denotes the infinitive tense.
The transformations I'm applying so far are:
chosen_word = "to crank (sth) up"
# To make the space between words double for better legibility
hidden_word = re.sub("s", " ", chosen_word)
# To hide the letters of the word
hidden_word = re.sub("[a-z]", "_ ", hidden_word)
But that results in:
_ _ _ _ _ _ _ ( _ _ _ ) _ _
How can I code a re.sub()
method that transforms all alphabetical characters to _
except the patterns to
and sth
and smb
?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…