简体   繁体   English

PHP正则表达式匹配任意数量的单词

[英]php regex match any number of words

I am new to php regex. 我是php正则表达式的新手。 I am trying to match any number of words. 我正在尝试匹配任意数量的单词。 For eg: A 'person name' can have any number of words, how can I do this? 例如:“人名”可以有任意数量的单词,我该怎么做?

I tried using \\w+\\ , but its matching only 1 word. 我尝试使用\\w+\\ ,但仅匹配1个单词。

DEMO 演示

You may try this, 你可以试试看

preg_match('~^\w+(?:\h+\w+)*$~', $str);

DEMO 演示

You can use the following for matching any number of words separated by whitespace: 您可以使用以下命令匹配由空格分隔的任意数量的单词:

[\w\s]+

See DEMO 演示

PHP: PHP:

preg_match('~^[\w\s]+$~', $str);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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