-1

I need to make an expression where there are no white spaces in the beginning, begins with street number that should be at least one digit and at most 4 digits and should end with a postal code of 6 digits. An example would be:

29 Younge Street M5E 1B2

I have this /^[^\s][a-zA-Z\s]+$/ but don't know how to add the extra conditions Thanks

Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
itsagam
  • 3
  • 4
  • 2
    Can you control where the data is coming from? Meaning the way the user is entering it? – user2182349 Oct 16 '15 at 02:19
  • How robust does this have to be? [As seen here](http://stackoverflow.com/questions/578406/what-is-the-ultimate-postal-code-and-zip-regex) postal codes can be tricky to validate with regex. If you just care about one region's, you might specify the criteria they follow. Also, how many words/characters can the street name be? – Gary Oct 16 '15 at 02:25
  • Woul you add examples of what your regex should match. `End with a postal code of 6 digits` is not valid to your example. To fit it it must be `end with a postal code of three chars a space and three chars` – Jorge Campos Oct 16 '15 at 02:25

1 Answers1

0

Your postal code look Canadian to me where the format is A1B 2C3 and if I recall correctly, Younge Street is a street in Montreal. Your requirements basically boils down to "start with 1 to 4 digits, end with the 6-character postal code". Use this:

^\d{1,4}\s+.*[A-Za-z]\d[A-Za-z]\s*\d[A-Za-z]\d$

Regex101

Code Different
  • 90,614
  • 16
  • 144
  • 163