1

I am so bad at creating regex and I'm struggling with what I am SURE it's a simple stupid regex.

I am using PHP to do this match. Here is what I have until now.

Test string: 8848842356063003
if(!preg_match('/^[0-2]|[7-9]{16}/', $token)) {
    return array('status' => 'failed', 'message' => "Invalid token", 'token' => '');
}

The regex must comply to this: Start with 0-2 or 7-9 and have EXACTLY 16 characters. What am I doing wrong? Because I get, as a match:

array(
  0 => 8
)

And I should get:

array(
  0 => 8848842356063003
) 

By the way: I am using PHP Live Regex to test my regex string.

Thanks in advance, Ares D.

1
  • 1
    You should read about subpatterns Commented Jul 9, 2015 at 11:01

1 Answer 1

2

The regex must comply to this: Start with 0-2 or 7-9 and have EXACTLY 16 characters

You can put starting numbers in same character class and use end anchor after matching 15 more charaters:

/^[0-27-9].{15}$/

If you want to match only digits then use:

/^[0-27-9]\d{15}$/
Sign up to request clarification or add additional context in comments.

2 Comments

PERFECT! THANK YOU! I don't know why I'm not getting this stupid regex stuff :( Is there a good book/tutorial that you can recommend?
I know that one... Unfortunately, there aren't that much examples for me to go with, so that I can understand fully. But thanks allot :) You have been more than helpful. I appreciate it, sir!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.