phpregexpreg-match

Get all alphanumeric words with length of either 4 or 12 characters


I have this text:

SU4R
C45G
G3HD
61U14XE7AR23 914K16W471LV V6SQ5V16LG91 24YL4HW956C3 UZ26J12K615V T741MH4N739W 31ST445G726H 621EH6VW7Q6M 55N629WJ945P 56TX2W6LC949 44DS765CF739 XC262HV1JZ6V 26YD4N1Y71F7 S4M3F1XeDC0D

I want to use preg_match to find specific type of code in that text, so I should contains:

I've ended with this:

preg_match("/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{4,12}/", $input_line, $output_array);

But:


Solution

  • Using preg_match_all(), something like this probably works
    http://www.phpliveregex.com/p/bz0

     # '/(?<!\S)(?i:[a-z\d]{4}|[a-z\d]{12})(?!\S)/'
    
     (?<! \S )                     # whitespace boundary
     (?i:                          # case insensitive cluster group
          [a-z\d]{4}                    # 4 alnum
       |                              # or
          [a-z\d]{12}                   # 12 alnum
     )
     (?! \S )                      # whitespace boundary