Home > Back-end >  I'm trying to create a regex validation for a street address that has the first letter of each
I'm trying to create a regex validation for a street address that has the first letter of each

Time:09-29

I have looked at a few examples and this almost works, but it won't allow numbers

\b[a-z]*

I am using google forms and want to validate that when someone puts in an address that all the first letters are capitalized in the word. If I use this to make sure that all lower case words are not in there it kind of works....but not really as any word that has a cap in the middle would also work.

So I want this to be validated

124 Main Street 
not
124 main Street
or
124 Main street 

CodePudding user response:

Here is a general regex pattern which should work:

^\d (?: [A-Z][a-z]*) $

Demo

This regex says to match:

^           from the start of the address
\d          a street number
(?:
    [ ]     space, followed by
    [A-Z]   a capital first letter
    [a-z]*  zero or more following lowercase letters
) 
$           the end of the address

CodePudding user response:

You could try negative lookahead assertion:

^(?!.*\s[a-z][a-z]). 

Explanation:

^ - anchor - match beginning of a string

(?!.*\s[a-z]{2}) - egative lookahead - assert what follows does not contain \s whitespace followed by two lowercase letters (so does not have word starting with lowercase letter)

. - match one or more of any character.

Regex demo

  • Related