I'm not that familiar with regex. Can someone tell me what this regex is trying to do? Does it return boolean or an array?
$res = preg_match('#^[A-Z]{1}( [0-9]{1,3})?((\.[0-9]{1,3}){0,2})?$#i', $value);
A breakdown...
^[AZ]{1}
- from the beginning of the line, match one char from AZ ... [AZ]
( [0-9]{1,3})?
- subgroup \\1 - match one space, then a number [0-9]
1-3 digits long {1,3}
, ?
make this one optional. ((\\.[0-9]{1,3}){0,2})?$
- subgroup \\3 (nested in \\2) - match literal .
then a number [0-9]
1-3 digits long {1,3}
, match 0-2 {0,2}
of this previous group, and optional due to ?
, the $
specifies that this match is done at the end of the line.
i
- end regex, set ignore case. Which means, for example the first [AZ]
could be [az]
without any change to the matches.
A few possible samples:
B 472.983.421 ( \1 = " 472" \2 = ".983.421" )
A ( \1 = "" \2 = "" )
C 18.1.1 ( \1 = " 18" \2 = ".1.1" )
D 0.0.0 ( \1 = " 0" \2 = ".0.0" )
d 0.0.0 ( \1 = " 0" \2 = ".0.0" ) # works due to the #i option.
And so on.
preg_match
always returns an int (1 if matched, 0 otherwise). If you want an array, it accepts a thrid parameter by reference that will be populated with the results.
# start of match string
^ starts with
[AZ]{1} starts with one uppercase letter
( [0-9]{1,3})? possibly followed by a space and 3 digits
((.[0-9]{1,3}){0,2})? possibly followed by a pattern of dot then 1-3 digits zero, one or two times
$ ends with (ie nothing after previous search critera
# end of match string
i case insensitive search
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.