tags:

views:

59

answers:

3

I want to match the values for A and C, but only if they are within the same paragraph.

A:one
C:foo

A:two
B:rofl, some rubbish :::

A:three
B:lol
C:bar

Right now I'm using

/A:([a-z]*).*?C:([a-z]*)/s

But that gives me "two" and "bar", which don't belong together. How do I exlude the empty line in my /.*?/ ?

+2  A: 

You'll have to disallow double-newlines. If your engine allows lookaheads:

/^A:([a-z]*)(?:(?!(?:\r?\n){2,}).)*^C:([a-z]*)/sm

This will work on Windows/UNIX newlines, but not with Mac's. This will also make sure that A and C are at the start of a line (note the m modifier).

Max Shawabkeh
You are right, thanks for the comment. You've got it, +1.
Cryo
A: 

This works for me:

/A:([a-z]*)\v*.*\v?C:([a-z]*)\v*/m

Basically enable newlines (remove . including newlines) and then specifically account for the newlines (\v) you expect in your pattern.

SoapBox
That will fail if there are more than 3 lines.
Max Shawabkeh
The question doesn't say anything about more than 3 lines.
SoapBox
A: 
$str=<<<A
A:one
C:foo

A:two
B:rofl, some rubbish :::

A:three
B:lol
C:bar
A;

$s = explode("\n\n",$str);
foreach($s as $k){
    if(strpos($k,"A:") !==FALSE && strpos($k,"C:") !==FALSE){
        $a = array_filter ( ( preg_split("/([A-Z]):|\n+/sm",$k) ) );
        print_r($a);
    }
}

output

$ php test.php
Array
(
    [1] => one
    [3] => foo
)
Array
(
    [1] => three
    [3] => lol
    [5] => bar
)
ghostdog74
This doesn't quite answer my question, but I finally setteled on this method of splitting into paragraphs beforehand. +1
blinry