[英]How to select strings which don't have special characters or numbers
我有成千上萬個字符串,我只想選擇其中沒有任何特殊字符的字符串。 特殊字符包括;:~?[]()+-_*^%$#@><{}\\|/
和數字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
}
任何幫助將不勝感激Ahmar
if (! preg_match('/[\'0-9^£$%&*()}{@#~?><>|=_+¬-]/', $string))
{
// No special characters or numbers found in this string.
}
構建自己的正則表達式以僅傳遞字母,空格和逗號。
^[a-zA-Z\s,]+$
要么
^[a-zA-Z\h,]+$
要么
^[\p{L}\h,]+$
\\h
匹配水平空格。 因此^[a-zA-Z\\h,]+$
匹配具有一個或多個空格或字母或逗號的行。 \\p{L}
可以匹配任何語言的任何字母。
if ( preg_match('~^[a-zA-Z\h,]+$~m', $string))
{
// do here
}
^[^;:~?\[\]()+_*^%$#@><{}\|\/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);
最有效的方法是檢測字符串是否包含至少一個不需要的字符:
使用范圍: 假設您僅處理ascii字符
if ( !preg_match('/[!-+--@[-`{-~]/', $str )) {
// the string is allowed
}
或更廣泛地使用: 帶有POSIX字符類
if ( !preg_match('/[^[:alpha:],[:space:]]/', $str )) {
// the string is allowed
}
//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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.