4

I have a string foo-foo-AB1234-foo-AB12345678. The string can be in any format, is there a way of matching only the following pattern letter,letter,digits 3-5 ?

I have the following implementation:

preg_match_all('/[A-Za-z]{2}[0-9]{3,6}/', $string, $matches);

Unfortunately this finds a match on AB1234 AND AB12345678 which has more than 6 digits. I only wish to find a match on AB1234 in this instance.

I tried:

preg_match_all('/^[A-Za-z]{2}[0-9]{3,6}$/', $string, $matches);

You will notice ^ and $ to mark the beginning and end, but this only applies to the string, not the section, therefore no match is found.

I understand why the code is behaving like it is. It makes logical sense. I can't figure out the solution though.

2 Answers2

1

You must be looking for word boundaries \b:

\b\p{L}{2}\p{N}{3,5}\b

See demo

Note that \p{L} matches a Unicode letter, and \p{N} matches a Unicode number.

You can as well use your modified regex \b[a-zA-Z]{2}[0-9]{3,5}\b. Note that using anchors makes your regex match only at the beginning of a string (with ^) or/and at the end of the string (with $).

In case you have underscored words (like foo-foo_AB1234_foo_AB12345678_string), you will need a slight modification:

(?<=\b|_)\p{L}{2}\p{N}{3,5}(?=\b|_)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Thanks a lot. I did try the `word boundaries` implementation but not to this extent. Works perfectly. –  Aug 03 '15 at 09:20
0

You have to end your regular expression with a pattern for a non-digit. In Java this would be \D, this should be the same in PHP.

Jan
  • 2,060
  • 2
  • 29
  • 34