0

Can Anyone help me understand what the below pattern means:

/^([a-zA-Z0-9]){1}([a-zA-Z0-9\-\._]){0,27}\.([a-zA-Z]){1}([a-zA-Z0-9]){1,3}/

What a string must have so that it bounds to the above pattern.

These preg_match patterns are so confusing to understand. ! We are using it to see that a filename conforms to above pattern.

Regards, Sagar

mmt
  • 161
  • 1
  • 2
  • 12
  • 3
    Try feeding the expression to http://regex101.com — that'll get you a basic understanding. – Amal Murali May 09 '14 at 07:56
  • 1
    [This visualization](http://i.stack.imgur.com/xXeDs.png) (from http://debuggex.com) might help as well. – Amal Murali May 09 '14 at 07:58
  • 1
    Must start (`^`) with a single (`{1}`) alphanumeric character (`[a-zA-Z0-9]`), followed by up to 27 (`{0,27}`) further characters allowing alphanumeric, hyphen, dot or underscore (`[a-zA-Z0-9\-\._]`); a dot for the extension separator (`\.`); then an extension comprising a single (`{1}`) alpha character (`[a-zA-Z]`) and between 1 and 3 (`{1,3}`) alphanumeric characters (`[a-zA-Z0-9]`)..... it should really have a terminating `$` as well to indicate that this pattern must be a match against the entire string, not just the first part of it, otherwise `A.BC$%^&*` would match as valid – Mark Baker May 09 '14 at 08:00
  • Thanks MArk :) You had explained it very well ! – mmt May 09 '14 at 08:29

1 Answers1

0

Basically, you are matching filenames that start with a letter or digit, followed by the characters letter, digit, -, ., _, any number of times up to 27, even 0 times. Then, there is a ., probably separating the extension, which is composed of a letter followed by the characters letter, digit any number of times from 1 to 3.

Some examples:

a.aa, 0_.A0a, A---____-_.a999

linkyndy
  • 14,398
  • 14
  • 96
  • 180