我有这样的文字:
string text = "Lorem ipsum dolor sit [1] amet, [3] consectetuer adipiscing [5/4/9] elit
Ut odio. Nam sed est. Nam a risus et est[55/12/33/4] iaculis";
我想获取一个字符串列表,其中包含所有[数字]或所有[数字/数字/ ...]的文本。
例如:
{"[1]","[3]","[5/4/9]","[55/12/33/4]"}
对于上面的文本。
我该如何使用正则表达式呢?
请您参考如下方法:
StringCollection resultList = new StringCollection();
Regex regexObj = new Regex(@"\[[\d/]*\]");
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
resultList.Add(matchResult.Value);
matchResult = matchResult.NextMatch();
}
说明:
\[ # match a literal [
[\d/]* # match any number of digits or /
\] # match a literal ]
