Get keys from template

后端 未结 7 991
忘掉有多难
忘掉有多难 2021-01-04 06:56

I would like to get a list of all possible keyword arguments a string template might use in a substitution.

Is there a way to do this other than re?

7条回答
  •  日久生厌
    2021-01-04 07:31

    Why do you want to avoid regular expressions? They work quite well for this:

    >>> re.findall(r'\$[a-z]+', "$one is a $lonely $number.")
    ['$one', '$lonely', '$number']
    

    For templating, check out re.sub, it can be called with callback to do almost the thing you want.

提交回复
热议问题