This question already has an answer here:
这个问题已经有了答案:
- Regex for string enclosed in <*>, C# 3 answers
- 用于<*>,c# 3的字符串
My input is
我的输入
This is <a> <test> mat<ch>.
Output should be
输出应该
1. <a>
2. <test>
3. <ch>
I have tried this
我试了
string input1 = "This is <a> <test> mat<ch>.";
var m1 = Regex.Matches(input1, @"<(.*)>");
var list = new List<string>();
foreach (Match match in m1)
{
list.Add(match.Value);
}
This returns <a> <test> mat<ch>
as single element in list.
这将返回
2 个解决方案
#1
10
Make your regex non greedy
让你的正则表达式不贪婪
var m1 = Regex.Matches(input1, @"<(.*?)>");
Or use negation based regex
或者使用基于拒绝的regex
var m1 = Regex.Matches(input1, @"<([^>]*)>");
#2
#1
10
Make your regex non greedy
让你的正则表达式不贪婪
var m1 = Regex.Matches(input1, @"<(.*?)>");
Or use negation based regex
或者使用基于拒绝的regex
var m1 = Regex.Matches(input1, @"<([^>]*)>");