0

In python you can format parameters using %-formatting like that:

"Foo %(bar)" % {"bar": baz}

I use some parameters like %(name)s for string rendering in Python (I am coding an auto mail apps) and I dont know how to evaluate it before using because if the user input the wrong parameter such as %(names or %names or %name)s or %%name()s or something like that, the apps will comes to error.

3
  • post some examples for valid and invalid strings. Commented Oct 21, 2014 at 4:08
  • So there are valid: %(foo), %(bar) but not %(foo)s, %(foo, %foo etc? Commented Oct 21, 2014 at 4:11
  • Only string in form of %(something)s is valid. Note that something can contains "%(" or ")s" but not both Commented Oct 21, 2014 at 4:13

1 Answer 1

1

This regex matches the "correct" input, eg %(foo)s:

^%\(\w+\)s$

This allows any "word" char for the name. To restrict the "name" part to just lowercase letters (as your examples all are):

^%\([a-z]+\)s$
Sign up to request clarification or add additional context in comments.

4 Comments

Op says the string in the middle can contain %( or )s but not both
@Smac89 I think English is not the OP's first language. Examples of bad input in question suggest this is what he wants.
@Bohemian: Smac89 is right. I also need to test that case. Sorry for not mentioning in the questions. Any suggestion?
But your comment says "Only string in form of %(something)s is valid". Please clarify what is valid and what is not valid by posting examples of each in your question.

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.