4
A
回答
10
正則表達式是這個(通常是)大規模矯枉過正。一個簡單的:
string replace = text.Replace(@"\b0", "</b>")
.Replace(@"\b", "<b>");
就足夠了。
+1
謝謝傑森,解決了。 – FadelMS
0
你並不需要爲這個正則表達式,你可以簡單地replace the values with String.Replace
.
但是,如果你想知道這到底是怎麼done with regex (Regex.Replace)這裏有一個例子:
var pattern = @"\\b0?"; // matches \b or \b0
var result = Regex.Replace(@"The quick \b brown fox\b0 jumps over the \b lazy dog\b0.", pattern,
(m) =>
{
// If it is \b replace with <b>
// else replace with </b>
return m.Value == @"\b" ? "<b>" : "</b>";
});
0
var res = Regex.Replace(input, @"(\\b0)|(\\b)",
m => m.Groups[1].Success ? "</b>" : "<b>");
0
作爲一個快速和骯髒的解決方案,我會做2次運行:首先用"</b>"
替換「\ b0」,然後用"<b>"
替換「\ b」。
using System;
using System.Text.RegularExpressions;
public class FadelMS
{
public static void Main()
{
string input = "The quick \b brown fox\b0 jumps over the \b lazy dog\b0.";
string pattern = "\\b0";
string replacement = "</b>";
Regex rgx = new Regex(pattern);
string temp = rgx.Replace(input, replacement);
pattern = "\\b";
replacement = "<b>";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(temp, replacement);
}
}
相關問題
- 1. 替換字符串值C的簡單問題#
- 2. C#的字符串替換問題
- 3. 字符串簡單替換
- 4. 簡單的字符串替換榆樹?
- 5. 簡單的BASH字符串替換
- 6. 簡單的PHP字符串替換?
- 7. Makefile中的簡單字符串替換
- 8. 簡單C++子字符串問題
- 9. 字符串替換問題
- 10. 字符串替換問題
- 11. 字符串替換問題
- 12. 字符串替換問題
- 13. 在Knockout中替換簡單字符串
- 14. 在簡單的Java字符串上替換替換錯誤
- 15. 簡單的PHP字符串的問題,
- 16. Perl的字符串替換問題
- 17. mysqldb.exeute中的字符串替換問題
- 18. 替換c字符串中的單詞
- 19. 在java問題中替換字符串
- 20. 簡單的Perl字符串問題
- 21. 爪哇 - 簡單的字符串問題
- 22. 在C#替換字符串
- 23. 字符串替換在C#
- 24. c中的字符串替換字符#
- 25. 替換字符串中的字符C
- 26. 替換字符串的單個字符
- 27. iphone:問題替換字符串
- 28. Sql字符串替換問題
- 29. 替換字符串有問題嗎?
- 30. Javascript:全部替換字符串問題
是什麼問題?你在尋找一個正則表達式生成器嗎? – 2011-07-05 17:05:06
是的。 Reqex表達。 – FadelMS
Steve,我遇到了一個問題,並試圖用不同的方法來解決它。到目前爲止沒有運氣。 – FadelMS