我有以下代码,用正则表达式将字符串分割:
string s = "test;3 régred";
string[] authorsList = Regex.Split(s, "(\\s+)|([\\p{P}\\p{S}])");
foreach (string q in authorsList)
{
Console.WriteLine(q);
}
预期是分离并仅保留:
test
3
régred
但它存储了:
test
;
3
*space*
régred
为什么它没有去掉分隔符?
@"(\s+)|([\p{P}\p{S}])"
。这样你就可以将正则表达式放入像RegExr或Regex101等在线工具中。 - Wai Ha Lee()
运行它,例如string[] authorsList = Regex.Split(s, "\\s+|[\\p{P}\\p{S}]");
查看 https://ideone.com/BczkoU - The fourth bird