[英]C# regex to remove BBCode color from string
string s = "I am an example of [color red]being colored[/color] at [color blue] any time[/color]";
如何检测所有 [color x] 和 [/color] 单词并将它们全部删除?
You can easily get the text with the below regex:您可以使用以下正则表达式轻松获取文本:
\[/?color\b[^][]*]
The regex matches:正则表达式匹配:
\\[/?
- a literal [
and an optional /
after it ( ?
means 1 or 0 repetitions ) - 一个文字[
和一个可选的/
在它之后( ?
表示1 或 0 次重复)color\\b
- a whole word color
( \\b
is a word boundary) color\\b
- 整个单词的color
( \\b
是单词边界)[^][]*
- 0 or more characters other than [
and ]
[^][]*
- 除[
和]
之外的 0 个或更多字符]
- a literal ]
]
- 文字]
See IDEONE demo看IDEONE 演示
string s = "I am an example of [color red]being colored[/color] at [color blue] any time[/color]";
Console.WriteLine(Regex.Replace(s, @"\[/?color\b[^][]*]", string.Empty));
Lucas Trzesniewski's solution works: Lucas Trzesniewski 的解决方案有效:
Regex.Replace(text, @"[/?color\\b.*?]", string.Empty) Regex.Replace(text, @"[/?color\\b.*?]", string.Empty)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.