简体   繁体   English

如何选择没有特殊字符或数字的字符串

[英]How to select strings which don't have special characters or numbers

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. 特殊字符包括;:~?[]()+-_*^%$#@><{}\\|/和数字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 应该选择句子1和4

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 任何帮助将不胜感激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,]+$

DEMO DEMO

\\h matches horizontal spaces. \\h匹配水平空格。 So ^[a-zA-Z\\h,]+$ matches the lines which has one or more spaces or alphabets or commas. 因此^[a-zA-Z\\h,]+$匹配具有一个或多个空格或字母或逗号的行。 \\p{L} would match any kind of letter from any language. \\p{L}可以匹配任何语言的任何字母。

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 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 使用范围: 假设您仅处理ascii字符

if ( !preg_match('/[!-+--@[-`{-~]/', $str )) {
 // the string is allowed
}

or for a more wide use: with POSIX character classes 或更广泛地使用: 带有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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM