当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Regex: Match thingies within paragraph

问题描述:

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 /.*?/ ?

网友答案:

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).

网友答案:

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.

网友答案:
$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
)
分享给朋友:
您可能感兴趣的文章:
随机阅读: