PHP Regex to find words containing both letters and numbers
php, regex
Solution
Such a code does either consist of
- an arbitrary count (minimum 1) of letters followed by one number and an arbitrary count (minimum 0) of letters and/or numbers
- or an arbitrary count (minimum 1) of numbers followed by one letter and an arbitrary count (minimum 0) of letters and/or numbers
written as a capture group:
((?:[a-zA-Z]+[0-9]|[0-9]+[a-zA-Z])[a-zA-Z0-9]*)
Problem
I am working on a PHP script that parses ads, and I want to add a regex to find special codes, that are words that can be any length, but consist of both letters and numbers in any order or any length. I am just not sure what the proper syntax would be for this. I have found patterns that allow either letters or numbers, or require specific patterns of letters and numbers, but not an almost random mix. Any advice would be greatly appreciated. Sample: Buy Widgets 20% discount with coupon code WID2010 by Friday Ideally, I'd want to detect the word "WID010" and use it to flag the item for other uses. That format, however, is not necessarily consistent. All that can be predicted is the codes always consist of at least one letter and one number, no spaces or punctuation.