我有一个很长的文本,在文本中有很多这样的东西(你好,你好)或(你好,你好),我必须考虑到空间。我如何在长文本中检测它们并检索hello和hi单词并从文本中添加到列表中?目前我使用这个正则表达式:
string helpingWordPattern = "(?<=\\()(.*?)(?<=\\))";
Regex regexHelpingWord = new Regex(helpingWordPattern);
foreach (Match m in regexHelpingWord.Matches(lstQuestion.QuestionContent))
{
// removing "," and store helping word into a list
string str = m.ToString();
if (str.Contains(","))
{
string[] strWords = str.Split(','); // Will contain a ) with a word , e.g. ( whole) )
if(strWords.Contains(")"))
{
strWords.Replace(")", ""); // Try to remove them. ERROR here cos i can't use array with replace.
}
foreach (string words in strWords)
{
options.Add(words);
}
}
}
我google并搜索正确的正则表达式,我使用的正则表达式也可以删除)但它没有。
答案 0 :(得分:3)
将\\(
\\)
括号匹配器放在您要捕获的组之外?
Regex regex = new Regex( "\\((.*?)\\)");
foreach (Match m in regex.Matches( longText)) {
string inside = Match.Groups[1]; // inside the brackets.
...
}
然后使用Match.Groups[1]
,而不是匹配的全文。
答案 1 :(得分:2)
有很多不同的方法可以做到这一点......下面是一些使用正则表达式进行匹配/拆分的代码。
string input = "txt ( apple , orange) txt txt txt ( hello, hi,5 ) txt txt txt txt";
List Options = new List();
Regex regexHelpingWord = new Regex(@"\((.+?)\)");
foreach (Match m in regexHelpingWord.Matches(input))
{
string words = Regex.Replace(m.ToString(), @"[()]", "");
Regex regexSplitComma = new Regex(@"\s*,\s*");
foreach (string word in regexSplitComma.Split(words))
{
string Str = word.Trim();
double Num;
bool isNum = double.TryParse(Str, out Num);
if (!isNum) Options.Add(Str);
}
}
答案 2 :(得分:2)
您也可以使用此正则表达式模式:
(?<=[\(,])(.*?)(?=[\),])
(?<=[\(,])(\D*?)(?=[\),]) // for anything except number
(?<=[\(,]) = Positive look behind, looks for `(`or `,`
(.*?) = Looks for any thing except new line, but its lazy(matches as less as possible)
(?=[\),]) = Positive look ahead, looks for `)` or `,` after `hello` or `hi` etc.
修改强>
您可以尝试以下示例代码:(未经测试)
List<string> lst = new List<string>();
MatchCollection mcoll = Regex.Matches(sampleStr,@"(?<=[\(,])(.*?)(?=[\),])")
foreach(Match m in mcoll)
{
lst.Add(m.ToString());
Debug.Print(m.ToString()); // Optional, check in Output window.
}