我有成千上万的字符串,其中我只想选择那些没有任何特殊字符的字符串。特殊字符包括;:~?[]()+-_*^%$#@><{}\|/
和数字0-9。所以基本上有效的句子是包含带逗号的字母或字母的句子。
最快的方法是什么,以便快速有效地完成任务。
示例:
1. She has the air of blank disdainful amusement a cat gets when toying with a mouse
2. Origin Expand 1535-1545 1535-45; disdain + -ful Related forms Expand disdainfully, adverb disdainfulness, noun Synonyms Expand contemptuous, haughty, contumelious
3. British Dictionary definitions for disdainful Expand disdainful /dsdenfl/ adjective
4.An example of someone who is disdainful, is a person saying they dislike someone just because of their religion
应选择第1和第4句
所以我需要一些
的内容if( $s does not have number an does not have special character)
{
//Save it
}
任何帮助将不胜感激 艾哈迈尔
答案 0 :(得分:1)
if (! preg_match('/[\'0-9^£$%&*()}{@#~?><>|=_+¬-]/', $string))
{
// No special characters or numbers found in this string.
}
答案 1 :(得分:0)
构建自己的正则表达式,只传递字母,空格,逗号。
^[a-zA-Z\s,]+$
OR
^[a-zA-Z\h,]+$
OR
^[\p{L}\h,]+$
\h
匹配水平空格。因此^[a-zA-Z\h,]+$
匹配具有一个或多个空格或字母或逗号的行。 \p{L}
会匹配任何语言的任何类型的信件。
if ( preg_match('~^[a-zA-Z\h,]+$~m', $string))
{
// do here
}
答案 2 :(得分:0)
^[^;:~?\[\]()+_*^%$#@><{}\|\/0-9-]+$
您也可以尝试这一点。参见演示。
http://regex101.com/r/oE6jJ1/32
$re = "/^[^;:~?\\[\\]()+_*^%$#@><{}\\|\\/0-9-]+$/im";
$str = "She has the air of blank disdainful amusement a cat gets when toying with a mouse \n\nOrigin Expand 1535-1545 1535-45; disdain + -ful Related forms Expand disdainfully, adverb disdainfulness, noun Synonyms Expand contemptuous, haughty, contumelious\n\nBritish Dictionary definitions for disdainful Expand disdainful /dsdenfl/ adjective\n\nAn example of someone who is disdainful, is a person saying they dislike someone just because of their religion";
preg_match_all($re, $str, $matches);
答案 3 :(得分:0)
最有效的方法是检测字符串是否包含至少一个您不想要的字符:
使用范围:这假设您只处理ascii字符
if ( !preg_match('/[!-+--@[-`{-~]/', $str )) {
// the string is allowed
}
或更广泛的用途:使用POSIX字符类
if ( !preg_match('/[^[:alpha:],[:space:]]/', $str )) {
// the string is allowed
}
答案 4 :(得分:0)
//your string variable:
$string = "She has the air of blank disdainful amusement a cat gets when toying with a mouse
Origin Expand 1535-1545 1535-45; disdain + -ful Related forms Expand disdainfully, adverb disdainfulness, noun Synonyms Expand contemptuous, haughty, contumelious
British Dictionary definitions for disdainful Expand disdainful /dsdenfl/ adjective
An example of someone who is disdainful, is a person saying they dislike someone just because of their religion";
//match function, and echo output
preg_match_all('/^[a-z, ]+$/gmi', $string, $matches);
foreach($matches[0] as $found){
echo $found . "\n"; //echoes sentences 1 and 4
}