TheBetterJORT
TheBetterJORT

Reputation: 808

regex with specific zip codes

I need help under the following situation: pr_zip MATCHES 'someregexhere'

The problem is I return 72701 and I don't want to return this zip. I only want to return these zips.

I thought about doing something like this

7(190).?%|7(191).?%|7(1949).?|7(195).?%7(1949).?%

but that seems clunky and there has to be a better way.

'7[1,2,5][0,7,8,9,5][0-9][0-9]%')

This doesn't work because it pulls more zipcodes than I need.

Anybody have an idea?

71901%
71902%
71903%
71909%
71910%
71913%
71914%
71949%
71951%
71956%
71964%
71968%
72087%
71834%
71937%
71839%
71840%
71854%
71724%
71730%
71731%
71747%
71749%
71750%
71758%
71759%
71762%
71765%
71768%
75501%
75503%
75504%
75505%
75507%
75559%
75561%
75567%
75569%
75570%
75573%
75574%
75599%

Upvotes: 1

Views: 142

Answers (2)

Bohemian
Bohemian

Reputation: 425398

There's no reason to finesse this; just use a plain old alternation:

\b(71901|71902|...etc)\b

Trying "minimise" the regex achieves nothing but obfuscates the zips, which makes reading and maintaining the regex next to impossible.

See KISS principle

Upvotes: 3

user557597
user557597

Reputation:

Here you go.

You can use a nice little tool to do it for you though Strings to Regex

7(?:1(?:7(?:24|3[01]|4[79]|5[089]|6[258])|8(?:3[49]|40|54)|9(?:0[1239]|1[034]|37|49|5[16]|6[48]))|2087|55(?:0[13457]|59|6[179]|7[034]|99))%

https://regex101.com/r/EePsaG/1

Formatted:

 7
 (?:
      1
      (?:
           7
           (?:
                24
             |  3 [01] 
             |  4 [79] 
             |  5 [089] 
             |  6 [258] 
           )
        |  8
           (?: 3 [49] | 40 | 54 )
        |  9
           (?:
                0 [1239] 
             |  1 [034] 
             |  37
             |  49
             |  5 [16] 
             |  6 [48] 
           )
      )
   |  2087
   |  55
      (?:
           0 [13457] 
        |  59
        |  6 [179] 
        |  7 [034] 
        |  99 
      )
 )
 %

Upvotes: 0

Related Questions