【问题标题】:Regex: remove non-alphanumeric chars, multiple whitespaces and trim() all together正则表达式:一起删除非字母数字字符、多个空格和 trim()
【发布时间】:2012-06-22 06:42:54
【问题描述】:

我有一个 $text 来去除所有非字母数字字符,用单个空格替换多个空格和换行符,并消除开始和结束空格。

到目前为止,这是我的解决方案。

$text = '
some-    text!! 

for testing?
'; // $text to format

//strip off all non-alphanumeric chars
$text = preg_replace("/[^a-zA-Z0-9\s]/", "", $text);

//Replace multiple white spaces by single space 
$text = preg_replace('/\s+/', ' ', $text);

//eliminate beginning and ending space
$finalText = trim($text);
/* result: $finalText ="some text for testing";
without non-alphanumeric chars, newline, extra spaces and trim()med */

是否可以在一个正则表达式中组合/实现所有这些?因为我会在如下一行中得到想要的结果

$finalText = preg_replace(some_reg_expression, $replaceby, $text);

谢谢

编辑:用测试字符串澄清

【问题讨论】:

  • @IgorChubin 显然你可以:)

标签: php regex


【解决方案1】:

我不认为你可以用一个正则表达式来实现。您基本上需要坚持if else 条件,仅通过正则表达式是不可能的。

您基本上需要一个正则表达式来删除非字母数字数字,另一个来折叠空格,这基本上是您已经在做的。

【讨论】:

  • 感谢专家的意见。经过几次努力的帮助,Igor Chubin也得出了同样的结论。可能是我一开始觉得我的问题不够清楚。
【解决方案2】:

如果这是您要查找的内容,请勾选 ---

$patterns = array ('/[^a-zA-Z0-9\s]/','/\s+/');
$replace = array ("", ' ');
trim( preg_replace($patterns, $replace, $text) );

可能需要一些修改,如果这是您想要做的事情,请告诉我??

【讨论】:

  • @AdnanShammout thx man ...至少你的回复比问卷调查更积极
【解决方案3】:

当然可以。这很容易。

重新看起来像:

((?<= )\s*)|[^a-zA-Z0-9\s]|(\s*$)|(^\s*)

我手头没有 PHP,我用过 Perl(只是为了测试 re 并证明它有效)(你可以使用我的代码 here):

$ cat test.txt 
         a       b       c    d
a b c e f g             fff  f

$ cat 1.pl 
while(<>) {
    s/((?<= )\s*)|[^a-zA-Z0-9\s]|(\s*$)|(^\s*)//g;
    print $_,"\n";
}

$ cat test.txt | perl 1.pl 
a b c d
a b c e f g fff f

对于 PHP 来说也是一样的。

什么是 RE?

((?<= )\s*)       # all spaces that have at least one space before them
|
[^a-zA-Z0-9\s]    # all non-alphanumeric characters
|
(\s*$)            # all spaces at the end of string
|
(^\s*)            # all spaces at the beginning of string

这里唯一棘手的部分是((?&lt;= )\s*)lookbehind assertion。当且仅当空格的子字符串之前有空格时,您才删除空格。

如果您想了解前瞻/后瞻断言的工作原理,请查看http://www.regular-expressions.info/lookaround.html

讨论更新

$text ='some ? ! ? text'; 时会发生什么? 然后生成的字符串在“some”和“text”之间包含多个空格。

解决这个问题并不是那么容易,因为需要具有可变长度的积极的后向断言,而这目前是不可能的。不能简单地检查空格,因为它可能会发生,因此它不是空格而是非字母数字字符,并且无论如何都会被删除(例如:在" !" 中,"!" 符号将被删除,但 RE 一无所知;一个需要(?&lt;=[^a-zA-Z0-9\s]* )\s* 之类的东西,但不幸的是这不起作用,因为 PCRE 不支持后向可变长度断言。

【讨论】:

  • 相当简洁,尤其是lookbehind断言。
  • 按要求工作,但与示例代码不同。文本之间的多个空格被匹配集的任何随机空格字符替换,而不是示例中的空格字符。能够做到这一点需要能够将一个字符替换为另一个字符,同时也将其他字符替换为任何内容。我不认为 PCRE 正则表达式可以同时做这两个。此外,lookbehind 明确需要一个空格,而不是任何空白字符,但这很容易解决。
  • 感谢伊戈尔·楚宾。我已经编辑了我的问题并添加了一个测试字符串来澄清。看来((?&lt;= )\s*)|[^a-zA-Z0-9\s]|(\s*$)|(^\s*) 不会做这些。
  • 可能我不明白你想得到什么,但据我所知,一切都很完美。请看:ideone.com/epDJW
  • @Martijn:你说得对,我想更正的唯一一点是:多个空格替换为集合中的 first 空格(你说过“任何随机空白字符")
【解决方案4】:

为了您自己的理智,您需要保留正则表达式,以便以后仍然可以理解和编辑:)

$text = preg_replace(array(
    "/[^a-zA-Z0-9\s]/", // remove all non-space, non-alphanumeric characters
    '/\s{2,}/', // replace multiple white space occurrences with single 
), array(
    '', 
    ' ',
), trim($originalText));

【讨论】:

    【解决方案5】:
    $text =~ s/([^a-zA-Z0-9\s].*?)//g;
    

    不必比这更难。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多