2011-01-30 126 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];