日期:2014-05-17  浏览次数:20436 次

正则表达式怎么写!
HTML code

 <div class="game_introduce">
       <div class="info bg_bor">
        <strong>阿萨德:</strong>
    <div class="howto">
            <ul>
         <li><span class="i_mouseleft">mouseleft</span>阿萨德</li>  </ul>
         </div>
          <p>发生的</p>
</div>     


我要得到里面的中文跟span里面的数据 我该怎么写正则

------解决方案--------------------
中文: [\u4e00-\u9fa5]+

span数据: (?i)(?<=<span[^>]*?>)[^<>]+(?=</span>)
------解决方案--------------------
C# code

            StreamReader reader = new StreamReader("c:\\1.txt",Encoding.Default);
            string source = reader.ReadToEnd();
            Regex reg = new Regex(@"(?is)([\u4e00-\u9fa5]+)|((?<=<span[^>]*?>).*?(?=</span>))");
            MatchCollection mc = reg.Matches(source);
            foreach (Match m in mc)
            {
                MessageBox.Show(m.Value);
            }

------解决方案--------------------
C# code
string html = @"
 <div class=""game_introduce"">
       <div class=""info bg_bor"">
        <strong>阿萨德:</strong>
    <div class=""howto"">
            <ul>
         <li><span class=""i_mouseleft"">mouseleft</span>阿萨德</li>  </ul>
         </div>
          <p>发生的</p>
</div>     
";
            foreach (var r in Regex.Matches(html, @"(?<=<span.*?\>)(.*?)(?=\</\s*?span\s*?\>)(.*?)\<\/li\>").Cast<Match>()) {
                string spanval = r.Groups[1].Value;
                string 中文 = r.Groups[2].Value;
            }

------解决方案--------------------
(?i)(?<=<span\sclass="game_introduce"[^>]*?>)[^<>]+(?=</span>)
------解决方案--------------------
StreamReader reader = new StreamReader("c:\\1.txt",Encoding.Default);
string source = reader.ReadToEnd();
Regex reg = new Regex(@"(?is)([\u4e00-\u9fa5]+)|((?<=<span[^>]*?>).*?(?=</span>))");
MatchCollection mc = reg.Matches(source);
foreach (Match m in mc)
{
MessageBox.Show(m.Value);
}
------解决方案--------------------
你可以匹配表达式,用协议就可以得到值,但不科学。
------解决方案--------------------
string html = @"
 <div class=""game_introduce"">
<div class=""info bg_bor"">
<strong>阿萨德:</strong>
<div class=""howto"">
<ul>
<li><span class=""i_mouseleft"">mouseleft</span>阿萨德</li> </ul>
</div>
<p>发生的</p>
</div>
";
foreach (var r in Regex.Matches(html, @"(?<=<span\s*class\=""i_mouseleft""\s*\>)(.*?)(?=\</\s*?span\s*?\>).*?([^\>]*?)\<\/li\>").Cast<Match>())
{
string spanval = r.Groups[1].Value;
string 中文 = r.Groups[2].Value;
}
------解决方案--------------------