Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing just the alphanumeric words with regex

Tags:

regex

I'm trying to find the regular expression to find just the alphanumeric words from a string i.e the words that are a combination of alphabets or numbers. If a word is pure numbers or pure characters I need to discard it.

like image 614
manny Avatar asked Jan 14 '10 18:01

manny


2 Answers

Try this regular expression:

\b([a-z]+[0-9]+[a-z0-9]*|[0-9]+[a-z]+[a-z0-9]*)\b

Or more compact:

\b([a-z]+[0-9]+|[0-9]+[a-z]+)[a-z0-9]*\b

This matches all words (note the word boundaries \b) that either start with one or more letters followed by one or more digits or vice versa that may be followed by one or more letters or digits. So the condition of at least one letter and at least one digit is always fulfilled.

like image 102
Gumbo Avatar answered Nov 23 '22 03:11

Gumbo


With lookaheads:

'/\b(?![0-9]+\b)(?![a-z]+\b)[0-9a-z]+\b/i'

A quick test that also shows example usage:

$str = 'foo bar F0O 8ar';
$arr = array();
preg_match_all('/\b(?![0-9]+\b)(?![a-z]+\b)[0-9a-z]+\b/i', $str, $arr);
print_r($arr);

Output:

F0O
8ar
like image 42
Mark Byers Avatar answered Nov 23 '22 05:11

Mark Byers