Home Address string (with special characters)

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
cohq82
Forum Commoner
Posts: 43
Joined: Mon Apr 21, 2008 8:38 pm

Home Address string (with special characters)

Post by cohq82 »

I am trying to create a RegEX to validate my home address string. The requirement is : must be more than 1 char and consist of any of the below char (not all required):

1 #
2 numbers(0 to 9)
3 alphabets(A/a to Z/z)
4 comma: ,
5 hyphen: -
6 underscore: _
7 dot: .
8 brackets: ( )
9 space
10 backslash: /



I thought this would be simple but I cannot figure out. Could you please help? There is no need to make this complicated so something like "111" would work. Just a string that allows all chars above.



Thanks
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Home Address string (with special characters)

Post by prometheuzz »

Something like this perhaps?

Code: Select all

$address = "Street Name 25 B";
if (preg_match('~^[-.,_()#/a-z\s\d]{2,}$~i', $address)) {
  echo "'$address' matched!";
}
Post Reply