michaellunsford Posted January 17, 2007 Share Posted January 17, 2007 I'm wanting to capture [0-9]{0,6} but [b]exclude[/b] the zip code (7[0-9]{4}) from the result.example 1 "78876 555 easy street" should return "555"example 2 "1095012 LBJ Highway, 71231" should return "1095012"example 3 "7000 East Main Street" should return "7000"Just can't figure it out. Quote Link to comment Share on other sites More sharing options...
effigy Posted January 17, 2007 Share Posted January 17, 2007 See if this works for the rest of your dataset. I changed the max to 7 since you said "1095012" should match--it is 7 numbers, not 6.[code]<pre><?php $tests = array( "78876 555 easy street", ### should return "555" "1095012 LBJ Highway, 71231", ### should return "1095012" "7000 East Main Street" ### should return "7000" ); foreach ($tests as $test) { preg_match('/\b(?!7\d{4})\d{1,7}\b/', $test, $matches); print_r($matches); }?></pre>[/code] Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted January 17, 2007 Author Share Posted January 17, 2007 [quote author=effigy link=topic=122751.msg506671#msg506671 date=1169011514]I changed the max to 7 since you said "1095012" should match--it is 7 numbers, not 6.[/quote]odd, my PHP reads it like this [1]{6}=7Great it works! Now, what's the difference between [0-9] and \d? preference, or is it more than that? Quote Link to comment Share on other sites More sharing options...
effigy Posted January 17, 2007 Share Posted January 17, 2007 [tt]\d [/tt]is a shortcut for[tt] [0-9][/tt]; I don't think it is affected by locales, like[tt] \w[/tt]. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.