用十进制表示替换所有十六进制数

Avi*_*ner 1 c# regex hex decimal

我有一个字符串:string s ="My Favorite numbers are: 42, 0x42, 24 and 0x24"
有没有办法使用正则表达式将所有十六进制数替换为它们的十进制表示?

对于上面的例子,我希望得到:"My Favorite numbers are: 42, 66, 24 and 36".

Dmi*_*nko 7

试试这个(正则表达式转换):

String source = "My Favorite numbers are: 42, 0x42, 24 and 0x24";

String result = Regex.Replace(source, @"0x(\d|[a-f]|[A-F])+", 
  (MatchEvaluator) (match => Convert.ToInt32(match.Value, 16).ToString()));
Run Code Online (Sandbox Code Playgroud)

我认为所有的十六进制数都是非负的足够小,int.