1

So i'm not great at regular expressions but i'd like to insert variables into a string. My string will look something like this (just a random example):

(%foo% * 3) / 2

And I would need to insert variables into it, %foo% being the name of the variable I need to use and might end up looking like this:

(4 * 3) / 2

I know I could do this using string formatting but each string may require different variables. Is there a good way that I can just create a regular expression that will swap out the placeholders with the variables?

0

1 Answer 1

2

You can still use string formatting with named arguments:

>>> "{foo}, {bar}".format(foo=2, bar=4)
'2, 4'
Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou. If I parse values but they are not used would it error?
@CallumBonnyman it's ok: "{foo}, {bar}".format(foo=2, bar=4, test=5) would result into 2, 4 too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.