Regex:([a-zA-Z0-9]+)@((?1))

Test string: abc def@abc

result:

Match 1: def
Match 2: abc

How to allow spaces in regex?
I want the result to be:

Match 1: abc def
Match 2: abc
有帮助吗?

解决方案

You want to add the space character inside your character class [] in order to be matched also.

$text = 'abc def@abc';

preg_match_all('/[a-z0-9 ]+/i', $text, $matches);
print_r($matches[0]);

Output

Array
(
    [0] => abc def
    [1] => abc
)

其他提示

try

preg_match("/^([a-zA-Z0-9 ])+$/i", $str)

want @ also match try

preg_match("/^([a-zA-Z0-9@ ])+$/i", $str) (i haven't test this)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top