【发布时间】:2015-12-12 16:42:05
【问题描述】:
问题与这些非常相似:
Check a string for containing a list of substrings
Check if a string contains a list of substrings and save the matching ones
有一个例外 - 不仅要检查而且要获取子字符串的起始索引以供将来处理。它可能看起来像 IndexOf 使用 List 的字符串:
private List<string> matches = new List<string> { "one", "two", "three" };
while (index < text.Length && -1 != (index = text.IndexOf(matches, index)))
{
...
// also I need to identify which one of substrings has been matched
index += matches[?].Length;
// further text processing...
}
换句话说,我需要知道文本字符串是否包含列表中的任何子字符串(不是单词!),如果包含,则获取匹配子字符串的开始和结束位置。
P.S:另外,这个方法必须足够快并且不区分大小写。
【问题讨论】:
-
你输入的字符串是什么?
-
您刚刚通过在标签中添加
Regex回答了您自己的问题...使用它!Match类为您提供了很多能力.. 比如获得Index或Length之类的.. -
我没有成功使用正则表达式。我能够匹配我的子字符串,但下一步是什么?如何获得职位? >> 喜欢获取索引或长度之类的东西.. 哦,我不知道。稍后会尝试...
-
>>你的输入字符串是什么? -- 取任何包含上面列出的子字符串的字符串。例如。 "twoBrownFoxJumpedThreetimesblabla oneblabla"