我写了一个正则表达式,功能是将网页中所有<script></script>对的内容获取出来,包括标签本身。
例如有段网页测试代码为
<script>aa</script>bb<script>cc</script>
理想中通过处理,应该得到的结果为
<script>aa</script>
<script>cc</script>
但是,使用我自己写的正则表达式<script>[\S\s]*</script>,得到的结果为
<script>aa</script>bb<script>cc</script>。源代码如下:
string Content = @"<script>aa</script>bb<script>cc</script>";
string MyRule = @"<script>[\S\s]*</script>";
Regex MyRegex = new Regex(MyRule, RegexOptions.IgnoreCase);
MyMatches = MyRegex.Matches(Content);
for (i = 0; i < MyMatches.Count; i++)
{
MessageBox.Show(MyMatches[i].ToString());
}求各位高手指点,我的正则表达式应该如何修改
例如有段网页测试代码为
<script>aa</script>bb<script>cc</script>
理想中通过处理,应该得到的结果为
<script>aa</script>
<script>cc</script>
但是,使用我自己写的正则表达式<script>[\S\s]*</script>,得到的结果为
<script>aa</script>bb<script>cc</script>。源代码如下:
string Content = @"<script>aa</script>bb<script>cc</script>";
string MyRule = @"<script>[\S\s]*</script>";
Regex MyRegex = new Regex(MyRule, RegexOptions.IgnoreCase);
MyMatches = MyRegex.Matches(Content);
for (i = 0; i < MyMatches.Count; i++)
{
MessageBox.Show(MyMatches[i].ToString());
}求各位高手指点,我的正则表达式应该如何修改
using System.Text.RegularExpressions;class Test
{
static void Main()
{
string Content = "<script> aa </script> bb <script> cc </script>";
string MyRule = "(?s)(?i)<script>.*?</script>";
Match MyMatch = Regex.Match(Content, MyRule);
while (MyMatch.Success)
{
Console.WriteLine(MyMatch.Value);
MyMatch = MyMatch.NextMatch();
}
}
}
using System.Windows.Forms;
using System.Text.RegularExpressions;class Test
{
static void Main()
{
string Content = "<script> aa </script> bb <script> cc </script>";
string MyRule = "(?s)(?i)<script>.*?</script>";
Match MyMatch = Regex.Match(Content, MyRule);
while (MyMatch.Success)
{
MessageBox.Show(MyMatch.Value);
MyMatch = MyMatch.NextMatch();
}
}
}
RegexOptions.Singleline 指定单行模式。更改点 (.) 的含义,使它与每一个字符匹配(而不是与除 \n 之外的每个字符匹配)。(?i) 等价于 RegexOptions.IgnoreCase。
(?s) 等价于 RegexOptions.Singleline。
限定符 *? 指定尽可能少地使用重复的第一个匹配(等效于 lazy *)。