简体   繁体   中英

Regular Expression That Contains At Least One Of Each

I'm trying to capitalize "words" that have at least one number, letter, and special character such as a period or dash.

Things like: 3370.01b , 6510.01.b , m-5510.30 , and drm-2013-c-004914 .

I don't want it to match things like: hello , sk8 , and mixed-up

I'm trying to use lookaheads, as suggested , but I can't get it to match anything.

$output = preg_replace_callback('/\b(?=.*[0-9]+)(?=.*[a-z]+)(?=.*[\.-]+)\b/i', function($matches){return strtoupper($matches[0]);}, $input);

You can use this regex to match the strings you want,

(?=\S*[a-z])(?=\S*\d)[a-z\d]+(?:[.-][a-z\d]+)+

Explanation:

  • (?=\\S*[az]) - This look ahead ensures that there is at least an alphabet character in the incoming word
  • (?=\\S*\\d) - This look ahead ensures that there is at least a digit in the incoming word
  • [az\\d]+(?:[.-][az\\d]+)+ - This part captures a word contain alphanumeric word containing at least one special character . or -

Online Demo

Here is the PHP code demo modifying your code,

$input = '3370.01b, 6510.01.b, m-5510.30, and drm-2013-c-004914 hello, sk8, and mixed-up';
$output = preg_replace_callback('/(?=\S*[a-z])(?=\S*\d)[a-z\d]+(?:[.-][a-z\d]+)+/i', function($matches){return strtoupper($matches[0]);}, $input);
echo $output;

Prints,

3370.01B, 6510.01.B, M-5510.30, and DRM-2013-C-004914 hello, sk8, and mixed-up

I don't think you never captured anything to put into matches...

$input = '3370.01b foo';
$output = preg_replace_callback('/(?=.*[0-9])(?=.*[a-z])(\w+(?:[-.]\w+)+)/i', function($matches){return strtoupper($matches[0]);}, $input);

echo $output;

Output

3370.01B foo

Sandbox

https://regex101.com/r/syJWMN/1

Regular expression:

https://regex101.com/r/sdmlL8/1

(?=.*\d)(.*)([-.])(.*)

PHP code:

https://ideone.com/qEBZQc

$input = '3370.01b';
$output = preg_replace_callback('/(?=.*\d)(.*)([-.])(.*)/i', function($matches){return strtoupper($matches[0]);}, $input);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM