2011-01-30 71 views
5

目前,我有這樣的:匹配高達X正則表達式或y的正則表達式

^(.+)\(\w+\)|^(.+)\s\(\d{3}\:\d{3}\s\-\s\d{3}\:\d{3}\) 

#1只匹配Foo的
#2 富具有這是正確的
#3不匹配FOO但它是在第三數組項[2]:

3rd array output: 
    (
     [0] => Foo (100:200 - 300:400) 
     [1] => 
     [2] => Foo 
    ) 


大膽的是什麼,我試圖匹配:
Foo的(match11)這個(100:200 - 300:400)結束#1
美孚擁有(not_matched)(100: 200 - 300:400)的端#2
美孚(100:200 - 300:400)的端#3
注:即時通訊不試圖匹配的#1,#2,# 3在每行的末尾,這只是供參考。

如果找到「(100:200-300:400)」,那麼在它前面得到任何文本,elseif「(not_matched)(100:200-300:400)」找到,然後在前面得到任何文本(100:200-300:400)「前面的文字」

elseif部分「(not_matched)(100:200-300:400)」可以被識別,因爲它只有1個白色not_matched的2個支架和之間的空間(100:200 - 300:400)


編輯:

這就是我想出了哪些似乎工作,雖然它需要在PHP中的一些解決方法是有用的。

(.+)\s\(\w+\)\s\(|(.+)\s\(\d{3}\:\d{3}\s\-\s\d{3}\:\d{3}\) 

工作例如:http://www.rubular.com/r/NSpGcnyg0p
出於某種原因,它似乎並沒有拯救我的例子,所以你必須複製/粘貼

但正則表達式沒有。直接匹配它們,這就是爲什麼我必須刪除php中的空數組元素,以便在[1]元素中獲得結果。

任何人都可以看到我在我的正則表達錯誤嗎?

+2

請讓你的問題更清晰...... – shybovycha 2011-01-30 08:40:33

+0

呀比較遺憾的是,有一點困惑,我試圖要做,認爲我已經清理了。 – Mint 2011-01-30 08:55:01

回答

1

試試這個:

^.*?(?=\s*(?:\(not_matched\)\s)?\(\d+:\d+\s*-\s*\d+:\d+\)) 

,或者在PHP:

if (preg_match(
    '/^     # Anchor the match at start of line 
    .*?     # Match any number of characters lazily 
    (?=     # until just before the following: 
    \s*     # - optional whitespace 
    (?:     # - the following group: 
     \(not_matched\)\s # - literal (not_matched), followed by 1 whitespace 
    )?     # (which is optional) 
    \(\d+:\d+\s*-\s*\d+:\d+\) # a group like (nnn:mmm - ooo:ppp) 
    )      # End of lookahead assertion 
    /x', 
    $subject, $regs)) { 
    $result = $regs[0]; 
} else { 
    $result = ""; 
} 
0

這將與您的第二個示例匹配:(.+)(\([\D]+\).+)(\#\d+)

而這一個將匹配另外兩個:(.+)(\([\d\W]+\).+)(\#\d+)

+0

關閉,但#2它匹配「(not_matched)」,我不想要。我只希望它匹配「Foo has」for#2 – Mint 2011-01-30 09:15:33

0

如果我清醒地認識到,這應該做的伎倆:

/^(.+)(?:\(not_matched\)\s)?(?:\(\d+:\d+\s-\s\d+:\d+\))\s.+\s(#\d+)$/i 
+0

第二個im試圖匹配「Foo has」,但是這個正則表達式也匹配我不想匹配的「(not_matched)」部分。陣列 ( [0] =>富具有(not_matched)(100:200 - 300:400)的端#2 [1] =>富具有(not_matched) [2] =>#2 ) – Mint 2011-01-30 09:23:33

0

這似乎工作,但需要一點點的解決方法是在PHP有用。 (閱讀我原來的問題)。

我會選擇這個作爲答案,如果沒有其他人有任何想法...

(.+)\s\(\w+\)\s\(|(.+)\s\(\d{3}\:\d{3}\s\-\s\d{3}\:\d{3}\) 
0

以下模式將匹配所有內容,結果存儲在wanted密鑰中:

$PATTERN = '/ 
    (?P<wanted>.*?)\s* # everything 
    (\(.+\s.+\)\s+)? # maybe followed by 
    (?= # that ends with 
     \(\d{3}:\d{3}\s-\s\d{3}:\d{3}\) 
    ) 
    /x'; 
preg_match($PATTERN, "Foo's (match11) this (100:200 - 300:400) the end", $matches); 
var_dump($matches['wanted']); 
preg_match($PATTERN, "Foo has (not matched) (100:200 - 300:400) the end", $matches); 
var_dump($matches['wanted']); 
preg_match($PATTERN, "Foo (100:200 - 300:400) the end", $matches); 
var_dump($matches['wanted']); 
0

Tripple checked,in multiple Versions。

<?php 

    $string[] = "Foo's (match11) this (100:200 - 300:400) "; 
    $string[] = "Foo has (not_matched) (100:200 - 300:400) "; 
    $string[] = "Foo (100:200 - 300:400) "; 

    $reg = "~(.*)(\([^\)]*\))?\s\(\d{3}\:\d{3}\s\-\s\d{3}\:\d{3}\)~iU"; 

    foreach ($string as $s){ 
     preg_match_all ($reg, $s, $m , PREG_SET_ORDER); 

     print "<br />String: ". $s . "<br /><pre>"; 
     print_r ($m); 
     print "</pre><br />OR"; 
    print "The String Required is: " . $m[0][1] . "<br />"; 
    } 

?> 

它的工作,你可以得到所需要的字符串上

$output = $m[0][1];