PHP preg_match查找整个单词 [英] PHP preg_match to find whole words

查看:164
本文介绍了PHP preg_match查找整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对正则表达式很陌生.您能帮我创建一个模式,该模式匹配整个单词并包含特定部分吗?例如,如果我有一个文本字符串执行正则表达式匹配",并且如果我搜索 express ,那么如果我搜索,它将为我提供 expression 表格,它应该可以给我执行,依此类推.有这个主意吗?

解决方案

preg_match('/\b(express\w+)\b/', $string, $matches); // matches expression
preg_match('/\b(\w*form\w*)\b/', $string, $matches); // matches perform,
                                                     // formation, unformatted

位置:

  • \b是单词边界
  • \w+是一个或多个单词"字符 *
  • \w*是零个或多个单词"字符

有关PCRE的信息,请参见转义序列上的手册. /p>


*注意:尽管不是真正的单词字符",但下划线_也包含在字符类\w中.

I'm quite new to regular expressions. Could you help me to create a pattern, which matches whole words, containing specific part? For example, if I have a text string "Perform a regular expression match" and if I search for express, it shuld give me expression, if I search for form, it should give me Perform and so on. Got the idea?

解决方案

preg_match('/\b(express\w+)\b/', $string, $matches); // matches expression
preg_match('/\b(\w*form\w*)\b/', $string, $matches); // matches perform,
                                                     // formation, unformatted

Where:

  • \b is a word boundary
  • \w+ is one or more "word" character*
  • \w* is zero or more "word" characters

See the manual on escape sequences for PCRE.


* Note: although not really a "word character", the underscore _ is also included int the character class \w.

这篇关于PHP preg_match查找整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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