0

我正在尝试修改此正则表达式模式,以使其不允许连续或开始/结束处的两个指定字符-

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

因此,目前它可以防止字符串中的任何位置出现这些字符,但我也想阻止这些字符发生以下情况:

  • 出现在字符串开头的任何空格、撇号'、下划线_或连字符-或点.

  • 还可以连续阻止这些字符中的任何两个,即''_._' -__- ' .

任何帮助将不胜感激。

非常感谢

4

3 回答 3

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!';
}
于 2013-02-20T11:32:33.117 回答
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);
于 2013-02-20T15:49:05.940 回答
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
于 2013-02-20T17:03:43.803 回答