简体   繁体   English

在字符类中包含量词

[英]include quantifier in character class

How am I able to add a quantifier inside a character class? 如何在字符类中添加量词? This is my current regular expression and what I want to achieve (besides what it is doing right now) is that whitespaces and dots (with an oncurrence of more than 2) will be matched and finally be removed using the preg_replace function 这是我当前的正则表达式,我想要实现的(除了现在正在做的事情)是将匹配空格和点(出现次数超过2),最后使用preg_replace函数将其删除

Current regular expression: 当前正则表达式:

[^A-Za-z0-9\s.\'\(\)\-\_]

Desired solution (notice the quantifier {1}): 所需的解决方案(请注意量词{1}):

[^A-Za-z0-9\s{1}.{1}\'\(\)\-\_]

Input (that has to be filtered): 输入(必须过滤):

Hi, this is a text.......that has    to be filtered!@#!

Output (After the regular expression): 输出(在正则表达式之后):

Hi this is a textthat hasto be filtered

There is no possibility for a quantifier inside a character class. 字符类中没有量词的可能性。 However you could use alternation and normal quantifiers, like 但是,您可以使用交替量和普通量词,例如

$str = "Hi, this is a text.......that has    to be filtered!@#!";
$pattern = "/[^A-Za-z0-9\\s.'()_-]|\\.{3,}|\\s{3,}/";
$subst = "";
print(preg_replace($pattern, $subst, $str));

Outputs: Hi this is a textthat hasto be filtered 输出: Hi this is a textthat hasto be filtered

You could also shorten the character class to [^\\\\w\\\\s.'()-] 您也可以将字符类缩短为[^\\\\w\\\\s.'()-]

In the regex [^A-Za-z0-9\\\\s.'()_-] matches any character that is not alphanumeric or whitespace or dot or round bracket or apostrophe or underscore or minus. 在正则表达式中[^A-Za-z0-9\\\\s.'()_-]匹配不是字母数字或空格,点或圆括号,撇号或下划线或负号的任何字符。 \\\\.{3,} matches any dot with a occurance of 3 or more (more than 2). \\\\.{3,}匹配任何出现3个或更多(大于2)的点。 \\\\s{3,} matches any whitespace with a occurance of 3 or more (more than 2) - note that this will match eg blank tab blank, as these are all whitespace characters. \\\\s{3,}与出现3个或更多(大于2)的任何空格匹配-请注意,这将匹配例如空白制表符空白,因为这些都是空白字符。

With the empty string substitution, everything matched will be replaced by an empty string (thus removed). 使用空字符串替换时,所有匹配的内容都将替换为空字符串(因此已删除)。

Another solution using preg_replace_callback function: 使用preg_replace_callback函数的另一种解决方案:

$str = "Hi, this is a text.......that has    to be filtered!@#!";

$replaced = preg_replace_callback(
        ["/(\s{3,}|\.{3,})/" , "/[^A-Za-z0-9\s.'()_-]/" ],
        function($m) { return ""; },
        $str);

print_r($replaced);

The output: 输出:

Hi this is a textthat hasto be filtered

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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