用String.Replace或Regex.Replace替换特定数字

肖恩·杜利(Sean Dooley)

在尝试对字符串中的特定数字进行字符串或正则表达式替换时遇到一点问题。

例如,在字符串中

@1 is having lunch with @10 @11

我想用如下所示的相应值替换“ @ 1”,“ @ 10”和“ @ 11”。

"@1" replace with "@bob"
"@10" replace with "@joe"
"@11" replace with "@sam"

所以最终的输出看起来像

"@bob is having lunch with @joe @sam"

尝试

String.Replace("@1", "@bob")

结果如下

@bob is having lunch with @bob0 @bob1

对解决方案有何想法?

帕纳焦蒂斯·卡纳沃斯(Panagiotis Kanavos)

假设所有占位符均以@数字开头且仅包含数字,则可以使用Regex.Replace重载,重载接受MatchEvaluator委托从字典中选择替换值:

var regex = new Regex(@"@\d+");

var dict = new Dictionary<string, string>
{
    {"@1","@bob"},
    {"@10","@joe"},
    {"@11","@sam"},
};

var input = "@1 is having lunch with @10 @11";
var result=regex.Replace(input, m => dict[m.Value]);

结果将是 "@bob is having lunch with @joe @sam"

与多次String.Replace通话相比,有一些优点

  1. 对于任意数量的占位符,代码更加简洁
  2. 您可以避免由于更换顺序而引起的错误(例如,@11必须先于@1
  3. 速度更快,因为您无需多次搜索和替换占位符
  4. 它不会为每个参数创建临时字符串。对于服务器应用程序来说这可能是一个问题,因为大量的孤立字符串将对垃圾收集器施加压力

之所以具有优势3-4,是因为正则表达式将解析输入并创建一个内部表示,其中包含任何匹配项的索引。是时候创建最终的字符串了,它使用StringBuilder从原始字符串中读取字符,但是在遇到匹配项时替换替换值。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

用string.replace(regex,value)替换变量名

来自分类Dev

Java使用.replace(x,y)替换字符串中的特定数字?

来自分类Dev

用Regex.Replace()替换字符串

来自分类Dev

用Regex.Replace替换公式中的变量名称

来自分类Dev

用变量中的特定数字替换任何数字

来自分类Dev

REGEXP REPLACE用/%替换%

来自分类Dev

Vim find and replace all occurrences of a regex string

来自分类Dev

使用String.replace(“”,“”);替换以逗号分隔的数字;

来自分类Dev

用preg_replace替换特定的字符组

来自分类Dev

String.Replace用双倍空格失败

来自分类Dev

regex使用postgres regexp_replace()用单引号替换反斜杠和单引号

来自分类Dev

在C ++ STL中,如何使用regex_replace从std :: string中删除非数字字符?

来自分类Dev

String.Replace和Regex.Replace无法在IComparer中使用特殊字符

来自分类Dev

如何在C#中使用regex.replace用反斜杠替换单个字符

来自分类Dev

cbind用数字替换String?

来自分类Dev

Ansible regex_replace

来自分类Dev

使用string.replace()将字符串替换为类似于字符串的数字

来自分类Dev

在Powershell c#中用Regex替换-replace

来自分类Dev

Regex.Replace 没有正确替换

来自分类Dev

使用preg_replace_callback用新值替换html内容中的特定标签

来自分类Dev

PHP str_replace用'-'替换&字符

来自分类Dev

PHP str_replace用'-'替换&字符

来自分类Dev

用Grunt-Replace替换HTML标签

来自分类Dev

用preg_replace(PHP / Regex)更改最后一段中的ID

来自分类Dev

regexp_replace 从数字中替换逗号

来自分类Dev

如何使用regex_replace?

来自分类Dev

Ruby regex search and replace array

来自分类Dev

regex to replace all tokens not in quotes?

来自分类Dev

将String.replace()与RegEx一起使用时,AS3是否支持功能?

Related 相关文章

  1. 1

    用string.replace(regex,value)替换变量名

  2. 2

    Java使用.replace(x,y)替换字符串中的特定数字?

  3. 3

    用Regex.Replace()替换字符串

  4. 4

    用Regex.Replace替换公式中的变量名称

  5. 5

    用变量中的特定数字替换任何数字

  6. 6

    REGEXP REPLACE用/%替换%

  7. 7

    Vim find and replace all occurrences of a regex string

  8. 8

    使用String.replace(“”,“”);替换以逗号分隔的数字;

  9. 9

    用preg_replace替换特定的字符组

  10. 10

    String.Replace用双倍空格失败

  11. 11

    regex使用postgres regexp_replace()用单引号替换反斜杠和单引号

  12. 12

    在C ++ STL中,如何使用regex_replace从std :: string中删除非数字字符?

  13. 13

    String.Replace和Regex.Replace无法在IComparer中使用特殊字符

  14. 14

    如何在C#中使用regex.replace用反斜杠替换单个字符

  15. 15

    cbind用数字替换String?

  16. 16

    Ansible regex_replace

  17. 17

    使用string.replace()将字符串替换为类似于字符串的数字

  18. 18

    在Powershell c#中用Regex替换-replace

  19. 19

    Regex.Replace 没有正确替换

  20. 20

    使用preg_replace_callback用新值替换html内容中的特定标签

  21. 21

    PHP str_replace用'-'替换&字符

  22. 22

    PHP str_replace用'-'替换&字符

  23. 23

    用Grunt-Replace替换HTML标签

  24. 24

    用preg_replace(PHP / Regex)更改最后一段中的ID

  25. 25

    regexp_replace 从数字中替换逗号

  26. 26

    如何使用regex_replace?

  27. 27

    Ruby regex search and replace array

  28. 28

    regex to replace all tokens not in quotes?

  29. 29

    将String.replace()与RegEx一起使用时,AS3是否支持功能?

热门标签

归档