0

Please help me to create the regular expression for the street address.
conditions are like
1. Only numbers,(/-) and [ABCDFGHJKLMNPRSTV] should allow.
2. Maximum two consecutive characters(letters) are allowed only after slash.
3. Should start with number only(max 4 numbers), rest is an optional if they enter: should follow the above mentioned criteria.
4. Possible combinations are like
12, 1567, 19/A, 123/B,12-45, 124-138, 12-14/A, 14/A-16, 12/A-16/B, 165/2, 123/1,
12-14/2, 14/2-16, 12/2-16/B, 11/A-12/1, 12/1-14/2, 0126/2-3, 125/BA, 345/BA/128,
248/12/A, 123/234/BA, 246/12/A/2

I tried with
"^\d{1,4}([/-]?\d{0,3}?[ABCDFGHJKLMNPRSTV]{0,2}?[/-]?\d{0,3}?
[ABCDFGHJKLMNPRSTVV]{0,2}?[/-]?\d{0,3}?[ABCDFGHJKLMNPRSTV]{0,2}?)$"

this code working for the above mentioned combinations but still allowing other combinations also,
character should not followed by "-(-C is wrong)" it should only followed by** '/'**
and i don't want enter more than 4 digits if its a single string**(Ex:: 1234 is right, now it is allowing 12345....to 13 chars)**
and only two characters allowed if its preceded with '/'(Now 12ABCDABCD.. allowing which is not right).

Please help me to create a proper regular expression, thanks in advance.

2
  • When you say [A,B,C...] it includes , too. You can just write [ABCDFGHJKLMNPRSTV]. Commented Feb 18, 2018 at 4:12
  • I hope this isn't for anything important, because regex is a bad tool for real street address validation. This article explains: smartystreets.com/articles/… Commented Aug 1, 2018 at 21:25

2 Answers 2

1

try this: ^[0-9]{1,4}(([\-\/][0-9]{1,4})|(\/[ABCDFGHJKLMNPRSTV]{1,2}))*$

  • [0-9]{1,4} : should start with numbers of length 1 to 4
  • [\-\/][0-9]{1,4}) : accepts - or / followed by numbers (1 to 4 length)
  • (\/[ABCDFGHJKLMNPRSTV]{1,2}) : accepts / followed by 1 or 2 of the characters inside the []
  • (([\-\/][0-9]{1,4})|(\/[ABCDFGHJKLMNPRSTV]{1,2}))*: to accept the last 2 pattern 0 or more times (one of them each time)
Sign up to request clarification or add additional context in comments.

Comments

0

Hope this will help

\d{1,5}\s\w.\s(\b\w*\b\s){1,2}\w*\.

This regex will validate address like

999 S. ABCD St.
eg: 123 N. Snow St.

Thanks,

1 Comment

regex should allow only 12, 1567, 19/A, 123/B,12-45, 124-138, 12-14/A, 14/A-16, 12/A-16/B, 165/2, 123/1, 12-14/2, 14/2-16, 12/2-16/B, 11/A-12/1, 12/1-14/2, 0126/2-3, 125/BA, 345/BA/128, 248/12/A, 123/234/BA, 246/12/A/2 these combinations. Please provide me the exact match, thanks for helping

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.