我需要一些帮助来定义如何编辑RichTextBox中的文本。 例如,我将在RichTextBox中粘贴以下文本:
"Release to user USER the roles: ZBR_POA_FIL_APOIO_GESTAO, ZBR_REC_FIL_SUPPORT, ZBR_RJO_CD_FIL_SUPPORT, ZBR_SVD_CD_FIL_SUPPORT, Z_GENERAL_OBJECTS, Z_DEBUG, Z_CHECK"
然后我想编辑它以仅保留包含下划线_
的文本,并将它们分隔成一个新行,如下所示:
ZBR_POA_FIL_APOIO_GESTAO
ZBR_REC_FIL_SUPPORT
ZBR_RJO_CD_FIL_SUPPORT
ZBR_SVD_CD_FIL_SUPPORT
Z_GENERAL_OBJECTS
Z_DEBUG
Z_CHECK
我想我应该为此使用正则表达式,但我没有太多运气。
答案 0 :(得分:1)
public string FormatString(string s)
{
String[] array=Regex.Matches(@"\b(?=\w*_)\w+\b",s)
.Cast<Match>()
.Select(x=>x.Value).ToArray();
return String.Join("\n",array);
}
答案 1 :(得分:0)
var text = @"Release to user USER the roles: ZBR_POA_FIL_APOIO_GESTAO, ZBR_REC_FIL_SUPPORT, ZBR_RJO_CD_FIL_SUPPORT, ZBR_SVD_CD_FIL_SUPPORT, Z_GENERAL_OBJECTS, Z_DEBUG, Z_CHECK";
var words = text.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
var wordsWithUnderscores = words.Where(o => o.Contains("_")).ToArray();
foreach(var word in wordsWithUnderscores) {
Console.WriteLine(word);
}