正则应用一例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Win.Regx
{
    
public partial class FrmImage : Form
    {
        
public FrmImage()
        {
            InitializeComponent();
        }

        
private void btn_Click(object sender, EventArgs e)
        {

            
string str = @"<IMG src='UploadFile/Mail/20083121g72134e194001.jpg' border=0> 
                <IMG src=""UploadFile/Mail/200812g1721341903301.png"" border=0> 
                <IMG src=""UploadFile/Mail/2008121g7213t419001.gif"" border=0> 
                <IMG src=""http://www.abc.com/121g7213t419001.jpg"" border=0> 
                <IMG src=""http://www.csdn.com/121g7213t419001.jpg"" border=0>
";
            Method1(str);
            Method2(str);
            Method3();
        }

        
/// <summary>
        
/// 方法一
        
/// 查找指定字符串中的图片名称
        
/// </summary>
        private void Method1(string str)
        {
            
// Regex ss = new Regex(@"(?<=UploadFile/Mail/).*?(?=\s*?['""])");
            Regex ss = new Regex(@"(?<=/)[a-zA-Z0-9]*\.(jpg|gif|png)(?=\s*?['""])");
            
// (?<=UploadFile/Mail/),表示反向预搜索,在这一位置之前为UploadFile/Mail/
            
// (?=\s*?['""]),表示正向预搜索,在这一位置之后为\s*?['""]
            MatchCollection mc = ss.Matches(str);
            
for (int i = 0; i < mc.Count; i++)
            {
                txt.AppendText(mc[i].Value 
+ "\r\n");
            }
            txt.AppendText(
"\r\n");
        }

        
/// <summary>
        
/// 方法二
        
/// 查找指定字符串中的图片名称
        
/// </summary>
        private void Method2(string str)
        {
            
//string pattern = @"(?i)<IMG src=(['""])UploadFile/Mail/(?<img>[^'""]+)(['""])[^>]*>";
            
//string pattern = @"(?i)<IMG src=(['""])UploadFile/Mail/(?<img>[^'""]+)\1[^>]*>";  // \1表示前面的(['""])匹配,这样更准确
            string pattern = @"<IMG src=(['""])UploadFile/Mail/(?<img>[^'""]+)\1[^>]*>"// \1表示前面的(['""])匹配,这样更准确
            Regex reg = new Regex(pattern);
            
foreach (Match m in reg.Matches(str))
            {
                txt.AppendText(m.Groups[
"img"].Value + "\r\n");
            }
            txt.AppendText(
"\r\n");
        }

        
private void Method3()
        {
            
// (?=xxx),正向预搜索,判断当前位置右侧是否能匹配指定表达式
            
// 输出1 2
            string reg = @"(\d+)(?=[^1-9])";
            
string test = "1t2t5";
            MatchCollection mc 
= Regex.Matches(test, reg);
            
for (int i = 0; i < mc.Count; i++)
            {
                txt.AppendText(mc[i].Value 
+ "\r\n");
            }
            txt.AppendText(
"\r\n");

            
// (?!xxx),正向预搜索否定,判断当前位置右侧是否不能够匹配指定表达式
            
// 输出5
            reg = @"(\d+)(?![^1-9])";
            test 
= "1t2t5";
            mc 
= Regex.Matches(test, reg);
            
for (int i = 0; i < mc.Count; i++)
            {
                txt.AppendText(mc[i].Value 
+ "\r\n");
            }
            txt.AppendText(
"\r\n");

            
// (?<=xxx),反向预搜索,判断当前位置左侧是否能够匹配指定表达式
            
// 输出2 5
            reg = @"(?<=[^1-9])(\d+)";
            test 
= "1t2t5";
            mc 
= Regex.Matches(test, reg);
            
for (int i = 0; i < mc.Count; i++)
            {
                txt.AppendText(mc[i].Value 
+ "\r\n");
            }
            txt.AppendText(
"\r\n");

            
// (?<!xxx),反向预搜索否定,判断当前位置左侧是否不能够匹配指定表达式
            
// 输出1
            reg = @"(?<![^1-9])(\d+)";
            test 
= "1t2t5";
            mc 
= Regex.Matches(test, reg);
            
for (int i = 0; i < mc.Count; i++)
            {
                txt.AppendText(mc[i].Value 
+ "\r\n");
            }
        }
    }
}
posted @ 2008-12-29 17:23  angushine  阅读(202)  评论(0编辑  收藏  举报