user1320260
user1320260

Reputation:

Regex match section within string

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.

Upvotes: 4

Views: 88

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

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|_)

Upvotes: 1

Jan
Jan

Reputation: 2070

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.

Upvotes: 0

Related Questions