PHP正则表达式查找包含字母和数字的单词 [英] PHP Regex to find words containing both letters and numbers

查看:53
本文介绍了PHP正则表达式查找包含字母和数字的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个解析广告的 PHP 脚本,我想添加一个正则表达式来查找特殊代码,这些代码可以是任意长度的单词,但由任意顺序或任意长度的字母和数字组成.我只是不确定正确的语法是什么.我发现了允许字母或数字的模式,或者需要特定模式的字母和数字,但不是几乎随机的组合.

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.

任何建议将不胜感激.

示例:在周五之前使用优惠码 WID2010 购买 Widgets 20% 折扣

Sample: Buy Widgets 20% discount with coupon code WID2010 by Friday

理想情况下,我想检测WID010"这个词并用它来标记该项目以供其他用途.然而,这种格式不一定一致.所有可以预测的是代码总是至少由一个字母和一个数字组成,没有空格或标点符号.

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.

推荐答案

这样的代码要么包含

  • 任意数量(最少 1 个)字母后跟一个数字以及任意数量(最少 0 个)字母和/或数字
  • 或任意数量(最少 1 个)后跟一个字母和任意数量(最少 0 个)字母和/或数字

写成捕获组:

((?:[a-zA-Z]+[0-9]|[0-9]+[a-zA-Z])[a-zA-Z0-9]*)

这篇关于PHP正则表达式查找包含字母和数字的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆