2013-02-20 78 views
0

我試圖修改此正則表達式模式,使其在一排或在開始/結束禁止兩個指定的字符 -正則表達式來禁止兩個字符連續

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

所以它可以防止瞬間這些字符的任意位置字符串中,但我也想從這些人物發生停止以下:

  • 任何空格,apostophes ',強調_或連字符-或點.出現在明星字符串

  • 的端噸也防止在一排這些字符中的任何兩個,即''_._' -__- ' .

任何幫助,將非常感激。

非常感謝

+0

你想修理,更換並進行..或在發現故障時,只需放棄? – Cups 2013-02-20 10:21:32

+0

發現錯誤,並去轟隆隆:)謝謝杯子 – Tim 2013-02-20 10:28:49

+0

關於這句話:*也阻止任何兩個這樣的字符在一行,即''或_._或'-__-'。*好,所以沒有一個單一的apostrophs在一排但是對於其他情況('_._'和''-__-'.'),你的意思是什麼? – kjetilh 2013-02-20 10:34:15

回答

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!'; 
} 
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); 
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 
相關問題