1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help with RegEx string..how can I get this element?

Discussion in 'General Programming Chat' started by simpleonline1234, Nov 14, 2011.

  1. simpleonline1234

    simpleonline1234 Junior Member

    Joined:
    Jan 26, 2010
    Messages:
    170
    Likes Received:
    13
    I am able to get ALL id's on the page but I wanted to be able to get this particular string of html

    Code:
    <input autocomplete="off" type="password" tabindex="3"  size="25" name="password" id="password" value=""  onfocus="_helpOn('help__password')" 
    Here is my current Regex: id=.*"

    This get ALL the freaking Id's on the page.

    I've tried using regexr but it's not giving me any results. Any ideas on how I can get this to only show me:


    id="password"

    Thanks
     
  2. termseo

    termseo Junior Member

    Joined:
    Nov 4, 2010
    Messages:
    103
    Likes Received:
    160
    Occupation:
    Software ingineer
    you get indexed to this string for ex : size="25" name="password" and make a substring from this index to (size="25" name="password" - "value"(index)
    like substring(ind1-(ind2-ind1)) the return will be id="password"
     
  3. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,638
    Likes Received:
    11,329
    Occupation:
    Pusillanimous Knitter
    Location:
    Buenos Aires
    What language are you using?
     
  4. paincake

    paincake Power Member

    Joined:
    Aug 18, 2010
    Messages:
    716
    Likes Received:
    3,100
    Home Page:
    Can you be more specific? Do you need to match the whole <input> tag that contains the id 'password'? Also, which language are you using?
     
  5. ranktrackerpro

    ranktrackerpro Newbie

    Joined:
    Dec 5, 2011
    Messages:
    12
    Likes Received:
    2
    It doesn't matter what programming language he wants to use. Regex is platform independent.

    Here you go:

    Code:
    id=["\S]+
    
    You're welcome :cool:

    Once you really learn how to use RegEx, your life will never be the same - seriously :)

    I highly recommend rubular - it's a great resource & testing platform for regular expressions.

    The above snippet in English means: take 1 or more non-whitespace characters, including the doublequote

    read the description on rubular - bottom of the page and you will learn more tricks for your scraping

    I don't know what you're trying to do exactly, but I think using XPath would be even easier.
     
    • Thanks Thanks x 1
  6. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,638
    Likes Received:
    11,329
    Occupation:
    Pusillanimous Knitter
    Location:
    Buenos Aires
    Nice one ranktrackerpro.

    I would have gone with this straight forward one
    but yours is more elegant :)

    The reason asking for the language are not the minor differences in regex implementations that appear, but to provide a snippet. So, if he had said C#, he 'd get something like this

    Code:
    bool FoundMatch = false;
    try {
    	FoundMatch = Regex.IsMatch(SubjectString, "id=\".+?\"");
    } catch (ArgumentException ex) {
    	// Syntax error in the regular expression
    }
    
     
  7. paincake

    paincake Power Member

    Joined:
    Aug 18, 2010
    Messages:
    716
    Likes Received:
    3,100
    Home Page:
    There are different flavors of regex.