正则表达式连续两个字符不允许

时间:2013-02-20 10:19:40

标签: php regex

我正在尝试修改此正则表达式模式,以便它不允许连续两个指定的字符或开头/结尾 -

/^[^\!\"\£\$\%\^\&\*\(\)\[\]\{\}\@\~\#\/\>\<\\\*]+$/

所以目前它在字符串中的任何地方阻止了这些字符,但我也希望阻止以下字符发生:

  • 在字符串结尾处出现的任何空格,撇号',下划线_或连字符-或点.

    < / LI>
  • 还会连续阻止这两个字符,即''_._' -__- ' .

非常感谢任何帮助。

非常感谢

3 个答案:

答案 0 :(得分:1)

单程

/^(?=[^!"£$%^&*()[\]{}@~#\/><\\*]+$)(?!.*[ '_.-]{2})[^ '_.-].*[^ '_.-]$/

注意,仅测试为javascript正则表达式,即

var rex = /^(?=[^!"£$%^&*()[\]{}@~#\/><\\*]+$)(?!.*[ '_.-]{2})[^ '_.-].*[^ '_.-]$/;
rex.test('okay');        // true
rex.test('_not okay');   // false

或者,匹配不允许的模式

/^[ '_.-]|[ '_.-]$|[!"£$%^&*()[\]{}@~#\/><\\*]|[ '_.-]{2}/

第一个正则表达式只匹配不包含不允许模式的字符串 上面的那个将匹配字符串中的任何不允许的模式。

更新

现在使用php进行简要测试。唯一的区别是字符集中的"需要转义。

<?php
$test = 'some string';
$regex = "/^[ '_.-]|[ '_.-]$|[!\"£$%^&*()[\]{}@~#\/><\\*]|[ '_.-]{2}/";
if ( preg_match( $regex, $test ) ) {
    echo 'Disallowed!';
}

答案 1 :(得分:0)

我不确定我是否理解确切的问题,但这是一个建议:

<?php
$test     = "__-Remove '' _._ or -__- but not foo bar '. _  \n";
$expected = 'Remove or but not foo bar';

// The list of disallowed characters. There is no need to escape.
// This will be done with the function preg_quote.
$excluded_of_bounds = "'_.-";

// Remove disallowed characters from start/end of the string.
// We add the space characters that should not be in the regexp.
$test = trim($test, $excluded_of_bounds . " \r\n");

// In two passes
$patterns = array(
  // 1/ We remove all successive disallowed characters,
  //    excepted for the spaces
  '#[' . preg_quote($excluded_of_bounds) . ']{2,}#',
  // 2/ We replace the successive spaces by a unique space.
  '#\s{2,}#',
);
$remplacements = array('', ' ');

// Go!
$test = preg_replace($patterns, $remplacements, $test);

// bool(true)
var_dump($expected === $test);

答案 2 :(得分:0)

$tests[1]     = "fail_.fail";  // doubles
$tests[]     = "fail_-fail";
$tests[]     = "fail_ fail";
$tests[]     = "fail  fail";
$tests[]     = "fail -fail";
$tests[]     = "pas.s_1";
$tests[]     = "pa.s-s_2"; // singles
$tests[]     = "pas.s_3";
$tests[]     = "p.a.s.s_4";
$tests[10]     = "pa s-s_5";
$tests[]     = "fail fail'"; // pre or post-pended
$tests[]     = " fail fail";
$tests[]     = " fail fail";
$tests[]     = "fail fail_";
$tests[15]     = "fail fail-";

// The list of disallowed characters. There is no need to escape.
// This will be done with the function preg_quote.
$exclude = array(" ","'", "_", ".", "-");

$pattern =  "#[" . preg_quote(join("", $exclude)) . "]{2,}#s";

// run through the simple test cases 

foreach($tests as $k=>$test){
if( 
    in_array(substr($test, 0, 1), $exclude)  
 || in_array(substr(strrev($test), 0 , 1) , $exclude))
   {
   echo "$k thats a fail" . PHP_EOL;
   continue;
   }

  $test = preg_match( $pattern,  $test);
    if($test === 1){
    echo "$k - thats a fail". PHP_EOL ;
    }else{
    echo "$k - thats a pass $test ". PHP_EOL ;
    }
}

从其他回复中无可救药地窃取,我主张使用PHP简单的in_array来首先检查字符串的开头和结尾,然后在发现不好的事情时尽早失败。

如果测试结束,那么运行一个非常简单的正则表达式。

将该批次粘贴到一个函数中并在失败时返回false - 这将是我添加的相当多的冗长行 - 您甚至可以将排除数组作为变量发送 - 但它看起来似乎是一个特定的函数所以可能是YAGNI

例如

if( badString($exclude_array, $input) ) // do stuff