Regex: Match thingies within paragraph
I want to match the values for A and C, but only if they are within开发者_如何学Python 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
)
精彩评论