我想在一个html文件中查找如下字符串:
<input type=HIDDEN name="uid" value="123456">
其中的123456可以任意匹配
如果找的到,把123456取出来。
要实现两个功能:
1:是否存在这个串
2:如果存在,把value的值(在这个例子中就是123456)取出来。
用的是TRegExpr类,以前没用过正则表达式,对于懂的人是很简单的,谢谢。

解决方案 »

  1.   

    Regular Expressions:Symbol Function
    \ Marks the next character as a special character. "n" matches the character "n". "\n" matches a linefeed or newline character.
    ^ Matches/anchors the beginning of line.
    $ Matches/anchors the end of line.
    * Matches the preceding character zero or more times.
    + Matches the preceding character one or more times.
    . Matches any single character except a newline character.
    (expression) Brackets or tags an expression to use in the replace command.A regular expression may have up to 9 tagged expressions, numbered according to their order in the regular expression.The corresponding replacement expression is \x, for x in the range 1-9.  Example: If (h.*o) (f.*s) matches "hello folks", \2 \1 would replace it with "folks hello".
    [xyz] A character set. Matches any characters between brackets.
    [^xyz] A negative character set. Matches any characters NOT between brackets.
    \d Matches a digit character. Equivalent to [0-9].
    \D Matches a nondigit character. Equivalent to [^0-9].
    \f Matches a form-feed character.
    \n Matches a linefeed character.
    \r Matches a carriage return character.
    \s Matches any white space including space, tab, form-feed, etc but not newline.
    \S Matches any nonwhite space character but not newline.
    \t Matches a tab character.
    \v Matches a vertical tab character.
    \w Matches any word character including underscore.
    \W Matches any nonword character.
    Note - ^ refers to the character '^' NOT Control Key + value.Examples:m.n matches "man", "men", "min" but not "moon".Te+st matches "test", "teest", "teeeest" etc. BUT NOT "tst".Te*st matches "test", "teest", "teeeest" etc. AND "tst".[aeiou] matches every lowercase vowel
    [,.?] matches a literal ",", "." or "?".
    [0-9, a-z] matches any digit, or lowercase letter
    [^0-9] matches any character except a digit (^ means NOT the following)You may search for an expression A or B as follow:"(John|Tom)"This will search for an occurrence of John or Tom.  There should be nothing between the two expressions.You may combine A or B and C or D in the same search as follows:"(John|Tom)(Smith|Jones)"
    This will search for John or Tom followed by Smith or Jones.Additionally:\p              Matches CR/LF (same as \r\n) to match a DOS line terminatorIf Regular Expression is not selected for the find/replace and in the Replace field the following special characters are also valid:Symbol        Function
    ^^        Matches a "^" character
    ^s        Is substituted with the selected (highlighted) text of the active file window.
    ^c        Is substituted with the contents of the clipboard.
    ^b        Matches a page break
    ^p        Matches a newline (CR/LF) (paragraph) (DOS Files)
    ^r        Matches a newline (CR Only) (paragraph) (MAC Files)
    ^n        Matches a newline (LF Only) (paragraph) (UNIX Files)
    ^t        Matches a tab characterNote - ^ refers to the character '^' NOT Control Key + value.
      

  2.   

    //提取Email地址的,你参考一下
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,RegExpr,HyperLinksDecorator;type
      TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        Memo2: TMemo;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);  private
        { Private declarations }
       r : TRegExpr;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses FReplace;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);var
      PrevPos : integer;
      Result,AText,MailTemplate:string;
     begin //memo1.Clear;
     AText:='';
     Result:=Memo1.Text;
     AText:=Result;
      PrevPos := 1;
      with TRegExpr.Create do
        try
        //-------------------------------------------------------------------------------
         PrevPos := 1;
         AText:=Result;
         Result:='';     MailTemplate:='(\[EMAIL=(\S+\@.[^\[]*)\])(.[^\[]*)(\[\/EMAIL\])'; //过滤email
         Expression := MailTemplate;
         if Exec (AText) then
          REPEAT
            Result := Result + System.Copy (AText, PrevPos,
             MatchPos [0] - PrevPos) + '&lt;img align=absmiddle src=pic/email1.gif&gt;'
             +'&lt;A HREF="mailto:'+match[2]+'" TARGET=_blank&gt;'+match[3]+'&lt;/A&gt;';
            PrevPos := MatchPos [0] + MatchLen [0];
          UNTIL not ExecNext;
         Result := Result + System.Copy (AText, PrevPos, MaxInt); // Tail
            finally Free;
       end;
          
       memo2.text:=Result;
     end;procedure TForm1.FormCreate(Sender: TObject);
    var r:TRegExpr;begin
     r := TRegExpr.Create;end;end.
      

  3.   

    正则表达式用于字符串处理,表单验证等场合,实用高效,但用到时总是不太把握,以致往往要上网查一番。我将一些常用的表达式收藏在这里,作备忘之用。本贴随时会更新。 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 匹配双字节字符(包括汉字在内):[^\x00-\xff] 应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;} 匹配空行的正则表达式:\n[\s| ]*\r 匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/ 匹配首尾空格的正则表达式:(^\s*)|(\s*$) 应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下: String.prototype.trim = function() 

        return this.replace(/(^\s*)|(\s*$)/g, ""); 
    } 利用正则表达式分解和转换IP地址: 下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的Javascript程序: function IP2V(ip) 

     re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g  //匹配IP地址的正则表达式 
    if(re.test(ip)) 

    return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1 

    else 

     throw new Error("Not a valid IP address!") 

    } 不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下: var ip="10.100.20.168" 
    ip=ip.split(".") 
    alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1)) 匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 匹配网址URL的正则表达式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)? 利用正则表达式去除字串中重复的字符的算法程序:[注:此程序不正确,原因见本贴回复] var s="abacabefgeeii" 
    var s1=s.replace(/(.).*\1/g,"$1") 
    var re=new RegExp("["+s1+"]","g") 
    var s2=s.replace(re,"") 
    alert(s1+s2)  //结果为:abcefgi 我原来在CSDN上发贴寻求一个表达式来实现去除重复字符的方法,最终没有找到,这是我能想到的最简单的实现方法。思路是使用后向引用取出包括重复的字符,再以重复的字符建立第二个表达式,取到不重复的字符,两者串连。这个方法对于字符顺序有要求的字符串可能不适用。 得用正则表达式从URL地址中提取文件名的javascript程序,如下结果为page1 s="http://www.9499.net/page1.htm" 
    s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2") 
    alert(s) 利用正则表达式限制网页表单里的文本框输入内容: 用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))" 用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))" 用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" 用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" 
    正则表达式,相关链接 
    http://blog.csdn.net/laily/category/19548.aspx 
    http://blog.csdn.net/laily/archive/2004/06/30/30525.aspx 微软的正则表达式教程(五):选择/编组和后向引用 http://blog.csdn.net/laily/archive/2004/06/30/30522.aspx 微软的正则表达式教程(四):限定符和定位符 http://blog.csdn.net/laily/archive/2004/06/30/30517.aspx 微软的正则表达式教程(三):字符匹配 http://blog.csdn.net/laily/archive/2004/06/30/30514.aspx 微软的正则表达式教程(二):正则表达式语法和优先权顺序 http://blog.csdn.net/laily/archive/2004/06/30/30511.aspx 微软的正则表达式教程(一):正则表达式简介 http://blog.csdn.net/laily/archive/2004/06/30/30360.aspx 小程序大作为:高级.找/替换、正则表达式练习器、Javascript脚本程序调试器 http://blog.csdn.net/laily/archive/2004/06/24/25872.aspx 经典正则表达式 正则表达式,正规表达式,正则表达式匹配,正则表达式语法,模式匹配,正规表达式匹配 javascript正则表达式 ASP正则表达式 ASP.NET正则表达式 C#正则表达式 JSP正则表达式 PHP正则表达式 VB.NET正则表达式 VBSCript正则表达式编程 delphi正则表达式 jscript 
    补充: 
    ^\d+$  //匹配非负整数(正整数 + 0) 
    ^[0-9]*[1-9][0-9]*$  //匹配正整数 
    ^((-\d+)|(0+))$  //匹配非正整数(负整数 + 0) 
    ^-[0-9]*[1-9][0-9]*$  //匹配负整数 
    ^-?\d+$    //匹配整数 
    ^\d+(\.\d+)?$  //匹配非负浮点数(正浮点数 + 0) 
    ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$  //匹配正浮点数 
    ^((-\d+(\.\d+)?)|(0+(\.0+)?))$  //匹配非正浮点数(负浮点数 + 0) 
    ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$  //匹配负浮点数 
    ^(-?\d+)(\.\d+)?$  //匹配浮点数 
    ^[A-Za-z]+$  //匹配由26个英文字母组成的字符串 
    ^[A-Z]+$  //匹配由26个英文字母的大写组成的字符串 
    ^[a-z]+$  //匹配由26个英文字母的小写组成的字符串 
    ^[A-Za-z0-9]+$  //匹配由数字和26个英文字母组成的字符串 
    ^\w+$  //匹配由数字、26个英文字母或者下划线组成的字符串 
    ^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$    //匹配email地址 
    ^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$  //匹配url 利用正则表达式去除字串中重复的字符的算法程序: var s="abacabefgeeii" 
    var s1=s.replace(/(.).*\1/g,"$1") 
    var re=new RegExp("["+s1+"]","g") 
    var s2=s.replace(re,"") 
    alert(s1+s2) //结果为:abcefgi 
    =============================== 
    如果var s = "abacabefggeeii" 
    结果就不对了,结果为:abeicfgg 
    正则表达式的能力有限 1.确认有效电子邮件格式 
    下面的代码示例使用静态 Regex.IsMatch 方法验证一个字符串是否为有效电子邮件格式。如果字符串包含一个有效的电子邮件地址,则 IsValidEmail 方法返回 true,否则返回 false,但不采取其他任何操作。您可以使用 IsValidEmail,在应用程序将地址存储在数据库中或显示在 ASP.NET 页中之前,筛选出包含无效字符的电子邮件地址。 [Visual Basic] 
    Function IsValidEmail(strIn As String) As Boolean 
    ' Return true if strIn is in valid e-mail format. 
    Return Regex.IsMatch(strIn, ("^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$") 
    End Function 
    [C#] 
    bool IsValidEmail(string strIn) 

    // Return true if strIn is in valid e-mail format. 
    return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); 

    2.清理输入字符串 
    下面的代码示例使用静态 Regex.Replace 方法从字符串中抽出无效字符。您可以使 1.确认有效电子邮件格式 下面的代码示例使用静态 Regex.IsMatch 方法验证一个字符串是否为有效电子邮件格式。如果字符串包含一个有效的电子邮件地址,则 IsValidEmail 方法返回 true,否则返回 false,但不采取其他任何操作。您可以使用 IsValidEmail,在应用程序将地址存储在数据库中或显示在 ASP.NET 页中之前,筛选出包含无效字符的电子邮件地址。 [Visual Basic] Function IsValidEmail(strIn As String) As Boolean ' Return true if strIn is in valid e-mail format. Return Regex.IsMatch(strIn, ("^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$") End Function [C#] bool IsValidEmail(string strIn) { // Return true if strIn is in valid e-mail format. return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); } 2.清理输入字符串 下面的代码示例使用静态 Regex.Replace 方法从字符串中抽出无效字符。您可以使 
      

  4.   

    楼主分数拿来
    regex : TRegExpr;
    regex.expression := '<input type=HIDDEN name="uid" value="(.*?)">';
    if regex.exec(网页文件) then
     结果:= regex.match[1];
    循环就不做了,楼主自己改吧