GabeL
GabeL

Reputation: 725

Python: Regex for words and hyphens

I'm using an API which until recently returned a dictionary with variables in the form of {{variable_name}}. I would then translate those variables into my own strings using a translation dictionary and this line:

new_string = re.sub(r"\{\{(\w+)\}\}", lambda m: translator[m.group(1)], raw_string)

However the API has changed, and for some odd reason now the variables are with hyphens instead of underscores, like {{variable-name}}.

Now with the hyphen my regex isn't matching, and after endless attempts to fix that and reviewing other Stack Overflow questions which failed me, I'm not sure what else I can try.

Long story short, I'm trying to match any series of word characters (letters, numbers, and underscore) and the hyphen character, surrounded by double curly brackets.

Upvotes: 0

Views: 5890

Answers (1)

Rahul
Rahul

Reputation: 2738

For variables in the form {{variable_name}} you used \w which is shorthand for [A-Za-z0-9_].

However the API has changed, and for some odd reason now the variables are with hyphens instead of underscores, like {{variable-name}}.

Now that _ underscore is out of question you should use character class [A-Za-z0-9-]. Note that - should come at end or beginning in character class to avoid it's interpretation as range.

If there is still possibility of _ in some cases then use [\w-] where \w is shorthand for word as mentioned above.

Hence your regex would be \{\{([\w-]+)\}\}

Upvotes: 2

Related Questions