/    Sign up×
Community /Pin to ProfileBookmark

Regular expression help

I am using this code for checking for a proper phone number.

[CODE]$string_exp = “^[0-9 .-]+$”;[/CODE]

I would also like to have the string be acceptable if they entered the characters N/A in place of a phone number. Can someone tell me how to change this expression to accept these three additional characters as an “or” statement to the phone number characters?

I don’t fully understand the finer points of regular expressions, and haven’t been able to figure this one out.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 19.2012 — You could do it as part of the regex, I suppose (adjusted for use with the preg_*() functions, which you should be using instead of ereg_*()?):
[code=php]
"#^(N/A|[0-9 .-])$#i"
[/code]
Copy linkTweet thisAlerts:
@lkeeneyauthorSep 25.2012 — This did not work, and I don't know why.

I don't understand the difference between preg_*() and ereg_*()?) functions and when and why I should use one over the other in a php file.

If someone can direct me to a place where I can learn more about this I would appreciate it.
Copy linkTweet thisAlerts:
@NogDogSep 25.2012 — I left out a repetition modifier:
[code=php]
<?php
$txt = 'N/A';
$result = preg_match('#^(N/A|[0-9 .-]+)$#i', $txt);
var_dump($result);
//int(1)
$txt = '123-456 789';
$result = preg_match('#^(N/A|[0-9 .-]+)$#i', $txt);
var_dump($result);
//int(1)
$txt = 'fubar';
$result = preg_match('#^(N/A|[0-9 .-]+)$#i', $txt);
var_dump($result);
//int(0)
[/code]

For more info: http://www.php.net/pcre
Copy linkTweet thisAlerts:
@lkeeneyauthorSep 25.2012 — I left out a repetition modifier:
[code=php]
<?php
$txt = 'N/A';
$result = preg_match('#^(N/A|[0-9 .-]+)$#i', $txt);
var_dump($result);
//int(1)
$txt = '123-456 789';
$result = preg_match('#^(N/A|[0-9 .-]+)$#i', $txt);
var_dump($result);
//int(1)
$txt = 'fubar';
$result = preg_match('#^(N/A|[0-9 .-]+)$#i', $txt);
var_dump($result);
//int(0)
[/code]

For more info: http://www.php.net/pcre[/QUOTE]


Thank you. I will try and study this to see if I can understand it.
Copy linkTweet thisAlerts:
@NogDogSep 25.2012 — A few things to help understand:

Parentheses enclose a sub-pattern, which is evaluated first (just like parentheses in PHP) and can have repetition modifiers append after the closing paren (not used in this case).

"|" is the "or" operator, so it matches if either side of the "|" matches.

I used "#" as the pattern delimiter, but it can be any of most special characters -- but you then have to escape that character within the pattern with a back-slash if you want that literal character to match. The "i" following the closing "#" makes the whole pattern case-Insensitive, so "N/A" or "n/a" would both match.

The "^" anchors the pattern to the start of the string, and the "$" to the end of the string.

[] is used to define a "character class": it matches on any of the characters or ranges of characters it contains. I used a "+" repetition modifier after it, meaning to match on one or more instances of those characters. ("*" would be used if you wanted to match on zero or more instances.)
×

Success!

Help @lkeeney 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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