Validating phone Singaporean number

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

Moderator: General Moderators

Post Reply
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Validating phone Singaporean number

Post by LonelyProgrammer »

Hi all,

The Singaporean phone number format starts with either a 8,6 or 9, and is followed by 7 digits. I tried this regex, but it does not work. Where did I go wrong?

Code: Select all

preg_match('/'^[9|6|8]\d{7,7}$'/i', '91111111 ', $result);
Thanks!
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Validating phone Singaporean number

Post by GeertDD »

Even though the regex itself is not that nicely written, it will match the phone numbers. However, look at the quotes around the pattern. I would be surprised if they do not result in a parse error.

Here is a cleaned up version:

Code: Select all

preg_match('/^[689]\d{7}$/D', '91111111', $result);
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Re: Validating phone Singaporean number

Post by LonelyProgrammer »

Thanks...it was originally a non-PERL compatible regex and I tried to convert it to one. Looks like I have done a clumsy job at that.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Validating phone Singaporean number

Post by prometheuzz »

LonelyProgrammer wrote:Thanks...it was originally a non-PERL compatible regex and I tried to convert it to one. Looks like I have done a clumsy job at that.
FYI, character classes always match one character and the "normal" regex meta characters don't apply inside them, so your: [9|6|8] matches either a '9', a '6', a '8' or a '|' (!), since the pipe-char has no meaning inside it.
Note that [968] is the same as (9|6|8), although when matching only one character, the first is preferred by many.
And your \d{7,7} is perfectly valid, but as already pointed out, the second 7 is not necessary.
So, all in all, you were pretty close! ; )
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Validating phone Singaporean number

Post by GeertDD »

prometheuzz wrote:Note that [968] is the same as (9|6|8), although when matching only one character, the first is preferred by many.
The first one is also faster.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Validating phone Singaporean number

Post by prometheuzz »

GeertDD wrote:
prometheuzz wrote:Note that [968] is the same as (9|6|8), although when matching only one character, the first is preferred by many.
The first one is also faster.
Albeit barely noticeable in this case.
The more important thing, IMO, is the readability of the regex (again: in this case, there are occasions when you'll have to take speed into consideration, of course).
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Validating phone Singaporean number

Post by GeertDD »

Yeah, the readability of [968] is much better too.
Post Reply