匹配 x 正则表达式或 y 正则表达式

5

我目前有这个:

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

#1 只匹配 Foo
#2 Foo has 是正确的
#3 匹配 foo,但它在第三个数组项 [2] 中:

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

我正在尝试匹配的内容为粗体字:
Foo's(match11)这个(100:200 - 300:400)结束#1
Foo has(不匹配)(100:200 - 300:400)结束#2
Foo(100:200 - 300:400)结束#3
注意:我并不是在尝试匹配每行末尾的#1,#2,#3,那只是作为参考。

如果发现了“(100:200 - 300:400)”则获取其前面的任何文本,否则如果发现“(not_matched) (100:200 - 300:400)”则获取其前面的任何文本,否则获取“(100:200 - 300:400)”前面的文本

elseif部分的“(not_matched) (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中复制/粘贴它。

但是正则表达式没有直接匹配它们中的任何一个,这就是为什么我必须在php中删除空数组元素,以便我可以在[1]元素中获得结果。

有人能看出我在正则表达式中做错了什么吗?


2
请您把问题表述得更清楚明确一些... - shybovycha
对不起啊,我有点糊涂了,不太确定自己想做什么,不过现在应该清楚了。 - Mint
6个回答

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 = "";
}

我知道这可以在一个正则表达式中实现,只是需要正确提出问题。谢谢。 - Mint

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

已经三次检查,在多个版本中。

<?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];

0

这似乎可以工作,但需要一些解决方法才能在php中有用。(请阅读我的原始问题)。

如果没有其他想法,我会选择这个作为答案...

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

0
这将匹配您的第二个示例:(.+)(\([\D]+\).+)(\#\d+)
而这个将匹配另外两个:(.+)(\([\d\W]+\).+)(\#\d+)

接近了,但是对于#2它匹配了“(not_matched)”,这不是我想要的。我只想让它匹配#2的“Foo has”。 - Mint

0
如果我理解得清楚,这应该可以解决问题:
/^(.+)(?:\(not_matched\)\s)?(?:\(\d+:\d+\s-\s\d+:\d+\))\s.+\s(#\d+)$/i

我正在尝试第二个正则表达式仅匹配"Foo has",但是这个正则表达式也匹配了"(not_matched)"部分,而我不想匹配它。数组 ( [0] => Foo has (not_matched) (100:200 - 300:400) the end #2 [1] => Foo has (not_matched) [2] => #2 ) - Mint

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接