/    Sign up×
Community /Pin to ProfileBookmark

Regular Expression Code Gallery

Does anyone know of a reliable site that would have pre-made regexps that could be used for a variety of applications. Personally, I’m interested in finding regexps for a basic contact information form: Name, Address, City, etc…

Thanks ?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@JonaNov 09.2004 — [font=trebuchet ms]Although not a common practice, I'm willing to be your human reference at this point; what exactly are you looking to validate? That the characters given are indeed letters and exclusively letters?[/font]

[code=php]
$string = "abc";
if(preg_match("/[a-zA-Z]/", $string)){
echo "Correct.";
} else {
echo "Incorrect.";
}
[/code]
Copy linkTweet thisAlerts:
@kouki0196authorNov 09.2004 — Hi, Jona.

Here are some examples of things that wouldn't fly with a simple letter-only expression:

address: 100 Massachuesetts Ave, NW.

address: Suite 800

name: Joe D'Angelo

name: Mary Smith-Cunning

I guess what I am looking for is an expression that will accept these types of answers, but strip the punctuation marks.

Thanks
Copy linkTweet thisAlerts:
@JonaNov 09.2004 — [font=trebuchet ms]Something like the following?[/font]

<i>
</i>/[a-zA-Z0-9,\'";@:]/
Copy linkTweet thisAlerts:
@AdamBrillNov 09.2004 — First off, why do /[a-zA-Z]/ instead of /a-z]/i? Not that it really matters, but...

Second, your regex(/a-zA-Z/) will only check if one of the characters in the string is an alpha character, it won't make sure that each character is. So the updated code would look like this:

[code=php]
$string = "abc";
if(preg_match("/^[a-z]*$/i", $string)){
echo "Correct.";
} else {
echo "Incorrect.";
}
[/code]
Or, for your second regex code example:
[code=php]
if(preg_match("/^[a-z0-9,'";@:]*$/i", $string)){
echo "Correct.";
} else {
echo "Incorrect.";
}
[/code]
Copy linkTweet thisAlerts:
@JonaNov 09.2004 — [font=trebuchet ms]D'oh! Thanks, Adam.[/font]
Copy linkTweet thisAlerts:
@AdamBrillNov 09.2004 — [i]Originally posted by Jona [/i]

[B][font=trebuchet ms]D'oh! Thanks, Adam.[/font] [/B][/QUOTE]
No problem. You better go get some sleep. ?
Copy linkTweet thisAlerts:
@JonaNov 09.2004 — [i]Originally posted by AdamBrill [/i]

[B]No problem. You better go get some sleep. ? [/B][/QUOTE]


[font=trebuchet ms]I'm on it. ? [/font]
Copy linkTweet thisAlerts:
@NogDogNov 09.2004 — [code=php]
$string = "John O'Donnell,123 Main St., Anywhere-Ville, NJ.";
# replace comma followed by 0 or more spaces with 1 space:
$search[0] = "/, */";
$replace[0] = "/ /";
# replace any other non-alpha, non-numeric, non-comma, non-space with nothing:
$search[1] = "/[^a-zA-Z0-9, ]/";
$replace[1] = "//";
# do the replace:
$cleanString = preg_replace($search, $replace, $string);
echo $cleanString;
[/code]
×

Success!

Help @kouki0196 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...