Best way to replace tokens in a large text template

后端 未结 10 1787
我在风中等你
我在风中等你 2020-12-08 17:14

I have a large text template which needs tokenized sections replaced by other text. The tokens look something like this: ##USERNAME##. My first instinct is just to use Stri

10条回答
  •  一个人的身影
    2020-12-08 17:31

    Had to do something similar recently. What I did was:

    • make a method that takes a dictionary (key = token name, value = the text you need to insert)
    • Get all matches to your token format (##.+?## in your case I guess, not that good at regular expressions :P) using Regex.Matches(input, regular expression)
    • foreach over the results, using the dictionary to find the insert value for your token.
    • return result.

    Done ;-)

    If you want to test your regexes I can suggest the regulator.

提交回复
热议问题