c# Regex match and replace using function

前端 未结 2 688
抹茶落季
抹茶落季 2021-01-27 00:29
/// 
/// Given HTML overlay for an image in the store, render it.
/// [p:n] renders as price for item ID n
/// 
/// Rendere         


        
相关标签:
2条回答
  • 2021-01-27 00:49

    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)))
    )
    
    0 讨论(0)
  • 2021-01-27 01:12

    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.

    0 讨论(0)
提交回复
热议问题