1
/// <summary>
/// Given HTML overlay for an image in the store, render it.
/// [p:n] renders as price for item ID n
/// </summary>
/// <returns>Rendered result</returns>
public static string RenderHTMLOverlay(string overlayHTML, int currencyID)
{
    const string pattern = "\\[p\\:(\\b\\d+\\b)\\]";
    overlayHTML = Regex.Replace(overlayHTML, pattern, FormatCurrency(GetItemPriceOnDate(DateTime.Now, currencyID, int.Parse("$1"))));

    return overlayHTML;
}

This doesn't work because $1 can't be passed as a parameter correctly to int.Parse.

Exception Details: System.FormatException: Input string was not in a correct format.

Does anyone know how I can work around this limitation?

2
  • 3
    Not related to the question, but I'd suggest using @"\[p\:(\b\d+\b)\]" instead if your expression for pattern. Means the same thing and is more readable. Commented Jul 30, 2012 at 16:55
  • 1
    Even better: @"\[p:(\d+)\]". The colon doesn't need escaping and the word boundaries are redundant. Commented Jul 30, 2012 at 20:29

2 Answers 2

3

You can only use the $1 notation if the replacement argument is a string, so you ended up passing $1 as a literal string to the int.Parse method.

Instead, use the (String, String, MatchEvaluator) overload with an anonymous method:

Regex.Replace(overlayHTML, pattern, 
match => FormatCurrency(GetItemPriceOnDate(DateTime.Now, currencyID, int.Parse(match.Groups[1].Value)))
)
Sign up to request clarification or add additional context in comments.

Comments

-1

I'm not totally sure I understand you, so bear with me if I am off.

 Console.WriteLine(int.Parse("$1"));  //throws exception Input string was not in a correct format.

 Console.WriteLine(int.Parse("$1".Replace("$", "")));  //Result: 1

If Store.CommonFunctions.GetItemPriceOnDate returns a string, you should be good to go.

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.