RegEx that starts with a certain div id

Discussion in 'Programming' started by FunkyFresh, May 22, 2012.

  1. #1
    I have this code as input.

    <div name="Planning Reviews"><h2>Planning - Reviews</h2>
    <ul>
    	<li><a href="http://example.edu/templates/sa-ath.rtf">saa-ath.rtf</a></li>
    </ul>
    </div>
    <div name="Design Reviews"><h2>Design - Reviews</h2>
    <ul>
    	<li>No templates are available for this Task</li>
    </ul>
    </div>
    <div name="Planning IP">
    	<h2>Planning - IP Waiver</h2>
    <ul>
    	<li><a onmousemove="prevHover(this)" href="http://example.edu/templates/wq9-26-07.rtf">asd 9-26-07.rtf</a></li>
    	<li><a onmousemove="prevHover(this)" href="http://example.edu/templates/qwe_2 party_072005.rtf">qw1_2 party_072005.rtf</a></li>
    </ul>
    </div>
    PHP:
    Now I am trying to use Regular Expression to find all div elmts that have a name that starts with the first word. For example, if I give the name "Planning" I want it to find all div elmts that have a name that starts with "Planning".

    preg_match_all("/<div name=\"{$wantedID}\">([^`]*?)<\/div>/", $file, $matches);
    PHP:
    The above expression works only if the 'name' matches exactly. I need help being modifying expression to find if 'name' starts with that word but can be anything after that to get the first and last div from above.
     
    FunkyFresh, May 22, 2012 IP
  2. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #2
    preg_match_all("/<div name=\"{$wantedID}[A-Za-z0-9_ ]+\">([^`]*?)<\/div>/", $file, $matches);
    
    PHP:
    This should match all the latin characters, numbers, dashes, underscores. If you need more characters, then add them.
    + sign at the end means that there are more that one characters.

    If you know, that you are looking for 3 to 6 characters after your $wantedId, then you would use: [A-Za-z0-9_ ]{3,6}
     
    e-abi, May 22, 2012 IP
  3. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #3
    e-abi, May 22, 2012 IP
  4. FunkyFresh

    FunkyFresh Peon

    Messages:
    499
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I tried that but $matches still spits out all the div regardless if name is 'Planning' or anything else
     
    FunkyFresh, May 22, 2012 IP
  5. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #5
    Try again, because testing it in the tool http://gskinner.com/RegExr/ with the following regex works: <div name="Planning[A-Za-z0-9_ ]+">([^`]*?)<\/div>
     
    e-abi, May 22, 2012 IP
  6. FunkyFresh

    FunkyFresh Peon

    Messages:
    499
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Tried it again, this time it worked. Thanks so much!
    that tool is pretty helpful too, it just took me a little time to figure out how it works
     
    FunkyFresh, May 23, 2012 IP