我试图在“*”字符上将字符串一分为二,我有以下句子:This is * test string
我写了这段代码
Regex regex = new Regex(@"\\*");
string[] substrings = regex.Split(text);
foreach (string match in substrings)
{
Console.WriteLine("'{0}'", match);
}
但我得到以下输出:
'T'h'i's' 'i's'....
但我希望:
'This is ' ' test string'
关于如何更正我的正则表达式的任何想法?
编辑:我的句子可能有多个“*”字符,在这种情况下我需要三个句子,例如:
This is * test *
最佳答案
从正则表达式中删除双重转义:
Regex regex = new Regex(@"\*");
...
Regex regex = new Regex(@"\*");
String text = "This is * test * more * test";
string[] substrings = regex.Split(text);
foreach (String match in substrings)
Console.WriteLine("'{0}'", match);
输出
'This is '
' test '
' more '
' test'
关于c# - 在 * 字符正则表达式上拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25047539/