Need RegEx help with my expression. Currently Not working.

Discussion in 'General Programming Chat' started by simpleonline1234, Apr 25, 2012.

  1. simpleonline1234

    simpleonline1234 Junior Member

    Joined:
    Jan 26, 2010
    Messages:
    170
    Likes Received:
    13
    I currently have a richtext (GetIt.text) that contain source code from a page. In the below example I am trying to grab anything between the two tags but I'm not sure how to set this up.

    My current Regex doesn't return a result.

    I am trying to grab anything between the value tags value="anything here"

    Any ideas?

    Thanks

    HTML line that contains the value="" that I need

    Code:
    <input id="hash" type="hidden" name="humanverify[hash]" value="e5a730f55f6c63196ae50559d6c5fa37" />
    
    
    This is the code that I am using but it doesn't produce anything.

    Code:
       Sub RegExGet()         
    ' The input string.          
    Dim value As String = "name=""humanverify[hash]"" value=""e5a730f55f6c63196ae50559d6c5fa37""
    
    ' Invoke the Match method.         
    Dim m As Match = Regex.Match(GETIT.Text, _                 
    "humanverify[hash]"" value=""([A-Za-z0-9\-]+)""", _                 
    RegexOptions.IgnoreCase)          
    
    ' If successful, write the group.         
    If (m.Success) Then             
    Dim key As String = m.Groups(1).Value             
    MessageBox.Show(key)         
    Else             
    MessageBox.Show("We Failed!!!")         
    End If     
    End Sub 
     
  2. theMagicNumber

    theMagicNumber Regular Member

    Joined:
    May 13, 2010
    Messages:
    347
    Likes Received:
    195
    Hi,
    [ and ] are operators in regex, so you have to escape them with \.
    Code:
    Sub RegExGet()
    
    
            Dim value As String = "name=""humanverify[hash]"" value=""e5a730f55f6c63196ae50559d6c5fa37"""
            Dim m As Match = Regex.Match(value, "humanverify\[hash\]"" value=""([A-Za-z0-9\-]+)""", RegexOptions.IgnoreCase)
            
        End Sub
    
     
    • Thanks Thanks x 1
  3. simpleonline1234

    simpleonline1234 Junior Member

    Joined:
    Jan 26, 2010
    Messages:
    170
    Likes Received:
    13
    Damn that was quick....thanks it works like a charm..One up for you my friend.
     
  4. kokoloko75

    kokoloko75 Elite Member

    Joined:
    Jan 1, 2011
    Messages:
    1,628
    Likes Received:
    1,943
    Occupation:
    Design director
    Location:
    Paris (France)
    Maybe it can help, in Delphi for this kind of job I use a simple parser function :
    Code:
    function Parse(T_, ForS, _T: string): string;var
      a, b: integer;
    begin
      Result := '';
      if (T_ = '') or (ForS = '') or (_T = '') then
        Exit;
      a := Pos(T_, ForS);
      if a = 0 then
        Exit
      else
        a := a + Length(T_);
      ForS := Copy(ForS, a, Length(ForS) - a + 1);
      b := Pos(_T, ForS);
      if b > 0 then
        Result := Copy(ForS, 1, b - 1);
    end;
    This function will return the value contained between two tags (strings).

    So, you'll use it like that :
    Code:
    var
      in, out: string;
    begin
      in := '<input id="hash" type="hidden" name="humanverify[hash]" value="e5a730f55f6c63196ae50559d6c5fa37" />';
      out := Parse('<input id="hash" type="hidden" name="humanverify[hash]" value="', in, '" />');
    end;
    It's in Delphi but I think it's easy to convert in VB...

    Beny
     
    • Thanks Thanks x 1