1

In my html I've serval token like this:

{PROP_1_1}, {PROP_1_2}, {PROP_37871_1} ...

Actually I replace that token with the following code:

htmlBuffer = htmlBuffer.Replace("{PROP_" + prop.PropertyID + "_1}", prop.PropertyDefaultHtml);

where prop is a custom object. But in this case it affects only the tokens ending with '_1'. I would like to propagate this logic to all the rest ending up with '_X' where X is numeric. How could I implement a regexp pattern to achieve this?

2 Answers 2

2

You can use Regex.Replace():

Regex rgx = new Regex("{PROP_" + prop.PropertyID + "_\d+}");
htmlBuffer = rgx.Replace(htmlBuffer, prop.PropertyDefaultHtml);
Sign up to request clarification or add additional context in comments.

Comments

2

You can do even better, you can catch both identifiers in a regular expression. That way you can loop through the references that exist in the string and get the properties for those, instead of looping through all the properties that you have and check if there is any reference for them in the string.

Example:

htmlBuffer = Regex.Replace(htmlBuffer, @"{PROP_(\d+)_(\d+)}", m => {
  int id = Int32.Parse(m.Groups[1].Value);
  int suffix = Int32.Parse(m.Groups[2].Value);
  return properties[id].GetValue(suffix);
});

Comments

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.