I have thousands of strings out of which I only want to select those which don't have any special characters in them. Special characters include ;:~?[]()+-_*^%$#@><{}\\|/
and numbers 0-9. So basically valid sentences are the ones which contain letters or letters with commas.
What would be fastest way to do it so that task can be done quickly and efficiently.
Example:
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
Sentence 1 and 4 should be selected
So I need something along the lines of
if( $s does not have number an does not have special character)
{
//Save it
}
Any help would be appreciated Ahmar
if (! preg_match('/[\'0-9^£$%&*()}{@#~?><>|=_+¬-]/', $string))
{
// No special characters or numbers found in this string.
}
Build your own regex to pass only letters, space, commas.
^[a-zA-Z\s,]+$
OR
^[a-zA-Z\h,]+$
OR
^[\p{L}\h,]+$
\\h
matches horizontal spaces. So ^[a-zA-Z\\h,]+$
matches the lines which has one or more spaces or alphabets or commas. \\p{L}
would match any kind of letter from any language.
if ( preg_match('~^[a-zA-Z\h,]+$~m', $string))
{
// do here
}
^[^;:~?\[\]()+_*^%$#@><{}\|\/0-9-]+$
You can try this as well.See demo.
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);
The most efficient way is to detect if the string contains at least one character you don't want:
using ranges: this assume you are only dealing with ascii character
if ( !preg_match('/[!-+--@[-`{-~]/', $str )) {
// the string is allowed
}
or for a more wide use: with POSIX character classes
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.