Regular Expression Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gregerly
    Recognized Expert New Member
    • Sep 2006
    • 192

    Regular Expression Question

    Hello All,

    I'm trying to do some HTML parsing using Regx, but I'm having some issues with my expression. I'm trying to match something like this:

    [HTML]<option value="1">Women 's 6
    <option value="2">Women 's 6.5[/HTML]

    My expression looks like this:

    [PHP]$msg_patt = "/<option value=\"(.*?)\" >([A-Za-z]+'[A-Za-z]{1} [0-9]+([\.0-9]{2})?)\n/";[/PHP]

    Using preg_match_all the above expression matches both of the options (women's 6 and women's 6.5).

    How can I change my expression so that it would match only Women's 6 (not matching Women's 6.5. I realize that removing the ? from the end of the pattern, it will Match, 6.5, but when I remove the ([\.0-9]{2})? it will still match Women's 6.5 (as it's matching the first part and disregarding the .5)

    Not sure how to approach this one using Regx.

    Thanks!

    Greg
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi Greg.

    You could simply have it look for the *end of line* after the number.
    That is, add the $ character after the number.

    Like
    [code=php]
    $regex = '/<option value=\"(.*?)\" >(\w+?\'\w{1} \d+)\s*$/';
    [/code]
    This would only allow white-spaces between the *integer* and the end of the line.

    Note that I replaced the [A-Za-z] class with \w. Does pretty much the same thing.

    Comment

    • gregerly
      Recognized Expert New Member
      • Sep 2006
      • 192

      #3
      Thanks Atli for your reply. I agree, what you have proposed would be the way to do it. For some reason, the match still isn't working, which is making me think there is something funky with my data. We are translating a website from english to Korean, and the HTML is funky (no closing tag for the options, etc...) but it doesn't seem to be picking up the new line char.

      Thanks again.

      greg

      Comment

      Working...