3

I want to check if phone no contains this pattern AABBCC Where A[0-9],B[0-9],C[0,9] They should be different e.g 112233,553322,887766

Let Us Suppose I Have a phone no 03334112233 It will say yes pattern matched.

PHP Code but It Is For Exact String

        $str = 'aabbaabbccaass'; //or whatever
        if (preg_match('/(?!.*?aabbcc)^.*$/', $str))
               echo "accepted\n";
        else
               echo "rejected\n";

Problem i don't know how to do if string is for numbers

Possible Duplicate but it does not contain answer and exact detail.

Edited : I want to match the last 6 characters of the string in this pattern AABBCC e.g 03329112233

Community
  • 1
  • 1
Fahid
  • 195
  • 1
  • 1
  • 8
  • Why on Earth would you wish to reject phone numbers with this pattern - that could be valid – Ed Heal Apr 13 '14 at 16:28
  • Basically these mobile numbers which are in this pattern AABBCC,ABABAB,AAABBCC are called MOBILE GOLDEN NUMBER.I have a database of mobiles no i want to check which numbers are golden number. – Fahid Apr 13 '14 at 18:20

4 Answers4

1

To match number with AABBCC format, you can use this pattern:

(?:(\d)\1(?!\1)){2}(\d)\2

example of use:

if (preg_match('/(?:(\d)\1(?!\1)){2}(\d)\2/', $str)
    echo "rejected\n";
else
    echo "accepted\n";

But if you have other tests to do (for example that there is only digits), it can be more flexible to use it in this way:

if (preg_match('/(?!.*(?:(\d)\1(?!\1)){2}(\d)\2)^\d+$/', $str)
    echo "accepted\n";
else
    echo "rejected\n";

pattern details:

(?:         # open a non capturing group that describes a repeated digit
    (\d)    # capture the first digit with group 1
    \1      # a backreference to group 1 (the same digit thus)
    (?!\1)  # check with a negative lookahead that the same digit doesn't follow
){2}        # repeat the group two times
(\d)\2      # same thing for digits 5 & 6 (the lookahead isn't needed here) 

Note that the digit in the capture group change at each repetition of the non capturing group (because the negative lookahead forces it).

Notice: if you want to reject numbers that contains, for example, 111122 or 112222 or 111111, you only need to remove the negative lookahead.
if you want to reject numbers with the format 112211 or 448844, you must change the pattern like this: (\d)\1(?!\d{0,2}\1)(\d)\2(?!\2)(\d)\3

Casimir et Hippolyte
  • 83,228
  • 5
  • 85
  • 113
  • Sorry Sir i forgot the mention I want to match the last 6 characters of the string in this pattern AABBCC.Now i edited the question. So in a case **03321122331** your regular expression will accept this string. – Fahid Apr 13 '14 at 18:44
0

You can use capturing groups and backreferences like this:

if (preg_match('/(?!.*(.)\1(.)\2(.)\3)^.*$/', $str))

The (.) will match any single character and assign it to a group. The first instance is assigned to group 1, the second to group 2 and so on. Later in the pattern, the backreference \1 will match exactly what was previously captured in group the first group, \2 will match what was captured in the second group, etc.

You probably will also want to use \d to match any single digit (it's only necessary to use this outside of the lookahead) and a {n,m} quantifier to match between n and m digits. For example, the following will match any sequence of 7 to 10 digits that does not contain a subsequence like AABBCC:

if (preg_match('/(?!.*(.)\1(.)\2(.)\3)^\d{7,10}$/', $str))
p.s.w.g
  • 136,020
  • 27
  • 262
  • 299
  • i am a new developer i don't know about regular expression please can u explain a little bit more about it. – Fahid Apr 13 '14 at 13:30
  • @Fahid I've tried to include links and a reasonable explanation. Is there something in particular in my answer you'd like me to expand upon? – p.s.w.g Apr 13 '14 at 13:32
  • Sorry sir i don't know why your answer was not showed me completely first time.that's way i asked.you explained well let me try. – Fahid Apr 13 '14 at 13:38
  • @Fahid Ahh, I've updated my answer for completeness. Still, I'd be happy to explain more if there's something that's not clear. – p.s.w.g Apr 13 '14 at 13:45
0

Your regex should be like this:

^((\d)\2){3}$

It is simpler and also works.

Al.G.
  • 3,929
  • 6
  • 32
  • 52
  • its great but it is not working e.g 666677 it also accept but in my case it should accept only Different numbers e.g 112233 – Fahid Apr 13 '14 at 13:55
0

As I understand, you only want to match the last 6 characters of the string, if they are digits, and of 3 all different digit pairs. Would also use a lookahead and some pattern like this:

(?>((\d)\2)(?!.*\1)){3}$
  • \2 checks for an equivalent of 2nd capturing group, which is one digit (shorthand \d)
  • using a negative lookahead to check, if not followed by .* any amount of any characters, followed by equivalent of 1st capturing group (which contains 2 equal digits).
  • {3} 3 repitions at $ end of string.

Test on regex101.com, Regex FAQ

Community
  • 1
  • 1
Jonny 5
  • 11,051
  • 2
  • 20
  • 42