By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

Regular Expression Question

gregerly
Expert 100+
P: 192
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
Oct 27 '08 #1
Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
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
Expand|Select|Wrap|Line Numbers
  1. $regex = '/<option value=\"(.*?)\">(\w+?\'\w{1} \d+)\s*$/';
  2.  
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.
Oct 27 '08 #2

gregerly
Expert 100+
P: 192
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
Oct 27 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.