我这里有多个
<ul name=""goods"" id="abc">
..........
<ul>如何获取ul中的.........如下是我的代码可是不好用。
MatchCollection mc = Regex.Matches(strBody, <ul name=""goods"" (.*?)<ul>", RegexOptions.IgnoreCase);
<ul name=""goods"" id="abc">
..........
<ul>如何获取ul中的.........如下是我的代码可是不好用。
MatchCollection mc = Regex.Matches(strBody, <ul name=""goods"" (.*?)<ul>", RegexOptions.IgnoreCase);
MatchCollection mc = Regex.Matches(strBody, @"<ul\s*name=""goods""[^<>]*>(?<need>[^<>]*)<ul>", RegexOptions.IgnoreCase);
Groups["need"]
using System.Text;
using System.Text.RegularExpressions;
class Test
{
static void Main()
{
string str = @"<ul name=""goods"" id=""abc"">
..........
<ul> ";
MatchCollection mc = Regex.Matches(str, @"<ul[\s\S]*?>([\s\S]*?)<ul", RegexOptions.IgnoreCase);//由于有换行。所以不能使用.*?,除非你先过滤掉\n\r
foreach (Match m in mc)
{
Console.WriteLine(m.Groups[1].Value.Trim());
}
Console.ReadKey();
}
}
string str = "";
string strBody = @"<ul name=""goods"" >.......... <ul> sd<ul name=""goods"" >...fff.... <ul>79";
MatchCollection m1 = Regex.Matches(strBody, @"<ul\s*name=""goods""[^<>]*>(?<need>[^<>]*)<ul>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach (Match m in m1)
{
str += m.Groups["need"].Value+"<br>";
}
Response.Write(str);输出结果:
..........
...fff....
这样更简约些 后面的参数RegexOptions.Singleline保证html换行时也能正确匹配到
http://www.jb51.net/article/3349.htm