Match up to x regex or y regex

I currently have this:

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

# 1 it matches only Foo
# 2 Foo has what is correct
# 3 matches foo , but it is in the element of the 3rd array [2]:

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


Bold is what I'm trying to match:
Foo (match11) this (100: 200 - 300: 400) end # 1
Foo has (not_matched) (100: 200 - 300: 400) end # 2
Foo (100: 200 - 300: 400) end # 3
note: im not trying to combine # 1, # 2, # 3 at the end of each line, this is just for reference.

"(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 (100: 200-300: 400)


Edit:

, , , , , php.

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

: http://www.rubular.com/r/NSpGcnyg0p
- , , , /.

, php, [1].

- , ?

+5
6

:

^.*?(?=\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 = "";
}
+1

: (.+)(\([\D]+\).+)(\#\d+).

: (.+)(\([\d\W]+\).+)(\#\d+).

0

, :

/^(.+)(?:\(not_matched\)\s)?(?:\(\d+:\d+\s-\s\d+:\d+\))\s.+\s(#\d+)$/i
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 (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
source

Checked triple in several versions.

<?php

    $string[] = "Foo (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 />";
    }

?>

It works, and you can get the desired line on

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

All Articles