Help with Regular Expression Pattern


  Can you help me with this regular expression pattern.


  Say in a code
preg_match('#^(\\d{4})(\\d\\d)(\\d\\d)$#', $data['expires'], $matches)
  Can anyone tell me what does it search for that pattern 
  Any help is highly appreciated.


Looks like it is searching for a string that starts with a digit (followed by 3 more digits), then 2 more digits, and ending with 2 more digits (8 digits total).

It is then grouping them as the first 4 digits in one group (likely to specify a year), followed by a group of 2 digits (likely month), followed by the last group of 2 digits (likely day).

about the meta characters:

@cpradio ;

Thanks for answering my concern.

However I have one more concern.

May I know what could possibly the value of the variable $data[‘expires’] in
this code

preg_match(‘#^(\d{4})(\d\d)(\d\d)$#’, $data[‘expires’], $matches)

before it searches the pattern ?

Coz I dont know what should be the value of $data[‘expires’] before it does
search the pattern.

I thought that the possible value was 2013-08-06 but it doesnt seems right.

Looking forward to hear from you soon?


To clarify what @cpradio; already explained,

  • Each \d represents one digit (0 through 9)
  • The \d{4} indicates exactly 4 digits (0 through 9)
  • The segments in parenthesis (called groups) will be provided in the $matches array

The first index of the array is the entire match, followed by each group: 4-digits then 2-digits then 2-digits. I would expect a 4 element array.

This indicates the expected pattern for this Regular Expression is in the form 00009944

As you stated, that would most likely be a date (particularly since the source variable is named ‘expires’!)

Note, also the caret at the beginning and the dollar sign at the end (as @cpradio; provided it) forces a match of EXACTLY 8 digits and nothing else.
In other words, if you were to use this Regular Expression to evaluate a string like “I will meet you on 20130415 at dusk.” it would not match it.

It would have to be 8 numbers/digits (as @ParkinT ; also mentioned), ideally in a specific format yyyyMMdd, for example. So the following would all be valid:

Things that are invalid

thanks a lot everyone. i got it fixed. i really appreciate your help.-