0
preg_match("/(11|10|12)([*0-9]+)/i", "11*&!@#") 

Above is the one i tried.

My requirement is total of 6 characters.

10**** 102*** 1023** 10234* 102345 

First two characters should be either 10 or 11 or 12 and the rest of the four characters should be like the pattern above.

How can i achieve it?

3 Answers 3

7
1[0-2][0-9*]{4} 

This should met your requirements:

  • 1 at the begining
  • then 0 or 1 or 2
  • then a digit or *, four times

EDIT

To avoid inputs like 102**5 you can do the pattern more complex:

1[0-2](([*]{4})|([0-9][*]{3})|([0-9]{2}[*]{2})|([0-9]{3}[*])|([0-9]{4})) 
Sign up to request clarification or add additional context in comments.

6 Comments

@tpaksu: I've edited my answer and add more complex solution to avoid wrong passes like yours
... Why not just remove the *? I don't get it. Can someone give me a real explanation as to why you would use * in square brackets?
Because that is the requirement from the question?
Oh. I guess I didn't fully understand the question, I thought OP ONLY wanted a six digit number with 10, 11 or 12 at the start.
I long would the pattern be if you are dealing with 16 digits .... 102*************5 ?? Your complex pattern would no longer be readable
|
3

Like this:

#(10|11|12)([0-9]{4})#

Outputs:

enter image description here

3 Comments

I think you forgot to add the * in your pattern.
What does the * denote in brackets?
Yes, we all wonder what that is, but if it doesn't mean random number, it means character *.
0

How about:

^(?=.{6})1[0-2]\d{0,4}\**$ 

this will match all your examples and not match strings like:

1*2*3* 

explanation:

The regular expression: (?-imsx:^(?=.{6})1[0-2]\d{0,4}\**$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- .{6} any character except \n (6 times) ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- 1 '1' ---------------------------------------------------------------------- [0-2] any character of: '0' to '2' ---------------------------------------------------------------------- \d{0,4} digits (0-9) (between 0 and 4 times (matching the most amount possible)) ---------------------------------------------------------------------- \** '*' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.