简体   繁体   English

从单词列表中将多个单词替换为字符串中的其他单词

[英]Replace multiple words to other words in a string from a list of words

This question is similar to: Replace multiple words in a string from a list of words 这个问题类似于: 从单词列表中替换一个字符串中的多个单词

The accepted solution there is the following 可接受的解决方案如下

string cleaned = Regex.Replace(input, "\\b" + string.Join("\\b|\\b",BAD_WORDS) + "\\b", "")

However, in my case I want to replace each "word" from the list with its bold version (basically add in the beginning and at the end) 但是,就我而言,我想用粗体替换列表中的每个“单词”(基本上在开头和结尾添加)

Regex.Replace(input, $@"\b(?:{string.Join("|",BAD_WORDS)})\b", "<b>$&</b>")

这是WiktorStribiżew建议的解决方案-完美地工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM