setcookie
setcookie

Reputation: 629

Regex to extract all words from text that have a least one number

How would I match all words that have at least one digit/number in multiline text block? I found this, Regular expression for a string that must contain minimum 14 characters, where at minimum 2 are numbers, and at minimum 6 are letters, which works for a single string. I get the concept of lookaheads, but not in my scenario since I make a preg_match_all(). Any hints?

Upvotes: 3

Views: 1451

Answers (1)

anubhava
anubhava

Reputation: 785196

You can use this regex for searching all words with at least a digit in it:

\b\w*?\d\w*\b

To make it unicode safe use:

/\b\w*?\p{N}\w*\b/u

RegEx Demo

Code:

$re = '/\b\w*?\p{N}\w*\b/u'; 
preg_match_all($re, $input, $matches);

Upvotes: 3

Related Questions