regex matching: checking whether an optional item exists
I've got data which can be like this:
LL DD L LL DDDD
or like:
LL DD L DDDD
where L is letter, D is digit
The DD group can be: "1"..."9","10"..."99"
with any number of spaces, dashes, or no space between any or them or some of them.
How c开发者_运维百科an I build a regex in php to detect all such cases?
I'm using this (which I can see is not perfect):
preg_match_all('/[A-Za-z]{2}( -\.)*?\d{1,2}( -\.)*?[A-Z]{1,3}( -\.)*?\d{4}/',$file,$matches);
Assuming D is a single digit and L a single letter:
$regex = '/([a-z]{2}( -\.)*[0-9]{2}( -\.)*[a-z]( -\.)*([a-z]{2}( -\.)*)?[0-9]{4})/Ui'
I think the pattern you want is
preg_match_all('/[a-z]{2}[ -]*\d{1,2}[ -]*[a-z]([ -]*[a-z]{2})?[ -]*\d{4}/i',$file,$matches);
This matches: two letters, then any number of spaces or dashes, followed by one or two digits, followed by any number of spaces or dashes, followed by a letter, optionally followed by (any number of spaces or dashes and followed by two letters), followed by any number of spaces or dashes, followed by four digits.
The /i
modifier makes it case-insensitive too.
try:
preg_match_all('[A-Za-z]{2}[-\s]*\d{1,2}[-\s]*[a-zA-Z][-\s]*[a-zA-Z]{2}[-\s]*\d{4}',$file,$matches);
EDIT:
my bad, I thought you mean whitespace (instead of space). the ff regex will take only spaces (instead of whitespace):
preg_match_all('[A-Za-z]{2}[- ]*\d{1,2}[- ]*[a-zA-Z][- ]*[a-zA-Z]{2}[- ]*\d{4}',$file,$matches);
Use the i
modifier to make the regular expression case-insensitive. This makes your regex a little shorter because you can write [a-z]
instead of [A-Za-z]
.
You can also surround each part that you want to extract out with parentheses and then retrieve them from the $matches
array.
My regex also assumes that all your numbers could vary in the number of digits. So, similar to DD
, the DDDD
number could be 0-9, 10-99, 100-999, or 1000-9999.
preg_match_all('/([a-z]{2})[- ]*(\d{1,2})[- ]*([a-z])[- ]*([a-z]{2})?[- ]*(\d{1,4})/i',$file,$matches);
$LL = $matches[1];
$DD = $matches[2];
//etc
精彩评论