Regexp:除了另一个正则表达式之外如何删除所有内容?

时间:2022-05-05 15:05:34

i have some regexp (like ^\w+[\w-.]\@\w+((-\w+)|(\w)).[a-z]{2,3}$, to match correct emails), but i cant figure out how to remove everything that doesn't match the regexp in my string.

我有一些正则表达式(如^ \ w + [\ w - 。] \ @ \ w +(( - \ w +)|(\ w))。[az] {2,3} $,以匹配正确的电子邮件),但我无法弄清楚如何删除与我的字符串中的正则表达式不匹配的所有内容。

Keeping the email example, i need a way to, given a sting like

保持电子邮件的例子,我需要一种方法,给一个刺痛的样子

$myString = "This is some text, the email is here example@example.com, and other things over here";

i need to return just 'example@example.com', or boolean false, if there is no email in the strings.

如果字符串中没有电子邮件,我需要返回'example@example.com'或布尔值false。

Of course, the email is just an example, some others times I'll need to remove everything except integer/floating numbers, etc...

当然,电子邮件只是一个例子,有些时候我需要删除除整数/浮点数等之外的所有内容......

I've googled around so much but didn't find anything.

我用Google搜索了这么多,但没有找到任何东西。

3 个解决方案

#1


If you surround your regex in parentheses and use preg_match or preg_match_all it will only return the part of the string that was matched by the regular expression:

如果用括号括起你的正则表达式并使用preg_match或preg_match_all,它将只返回正则表达式匹配的字符串部分:

$myString = "This is some text, the email is here example@example.com, and other things over here";
preg_match("/(\w+[\w-.]\@\w+((-\w+)|(\w)).[a-z]{2,3})/", $myString, $matches);
print_r($matches);

Note I also took off the beginning and end of string delimeters as in this case they are not needed.

注意我也取消了字符串分隔符的开头和结尾,因为在这种情况下它们不是必需的。

#2


Use the preg_match_all function to match all occurrences. The preg_match_all function itself returns the number of matches or false if an error occured.

使用preg_match_all函数匹配所有匹配项。 preg_match_all函数本身返回匹配数,如果发生错误则返回false。

So:

$myString = "This is some text, the email is here example@example.com, and other things over here";
if (($num = preg_match_all('/\w+[\w-.]\@\w+((-\w+)|(\w)).[a-z]{2,3}/', $myString, $matches)) !== false) {
    echo $num.' matches have been found.';
    var_dump($matches);
} else {
    // error
}

#3


Your regex needs to have $ and ^ removed in order to work, those symbols cannot be found in the middle of the string. If your regex matches it cannot possibly contain anything else but email.

你的正则表达式需要删除$和^才能工作,这些符号在字符串的中间找不到。如果您的正则表达式匹配,它除了电子邮件之外不可能包含任何其他内容

#1


If you surround your regex in parentheses and use preg_match or preg_match_all it will only return the part of the string that was matched by the regular expression:

如果用括号括起你的正则表达式并使用preg_match或preg_match_all,它将只返回正则表达式匹配的字符串部分:

$myString = "This is some text, the email is here example@example.com, and other things over here";
preg_match("/(\w+[\w-.]\@\w+((-\w+)|(\w)).[a-z]{2,3})/", $myString, $matches);
print_r($matches);

Note I also took off the beginning and end of string delimeters as in this case they are not needed.

注意我也取消了字符串分隔符的开头和结尾,因为在这种情况下它们不是必需的。

#2


Use the preg_match_all function to match all occurrences. The preg_match_all function itself returns the number of matches or false if an error occured.

使用preg_match_all函数匹配所有匹配项。 preg_match_all函数本身返回匹配数,如果发生错误则返回false。

So:

$myString = "This is some text, the email is here example@example.com, and other things over here";
if (($num = preg_match_all('/\w+[\w-.]\@\w+((-\w+)|(\w)).[a-z]{2,3}/', $myString, $matches)) !== false) {
    echo $num.' matches have been found.';
    var_dump($matches);
} else {
    // error
}

#3


Your regex needs to have $ and ^ removed in order to work, those symbols cannot be found in the middle of the string. If your regex matches it cannot possibly contain anything else but email.

你的正则表达式需要删除$和^才能工作,这些符号在字符串的中间找不到。如果您的正则表达式匹配,它除了电子邮件之外不可能包含任何其他内容