preg_match_all syntax problem
Having trouble with preg_match syn开发者_JAVA百科tax
with in a page I need to find anything like
$first = '/>http:\/\/www.(.*?)\/(.*?)\</';
$second = '/="http:\/\/www.(.*?)\/(.*?)"/';
How could I combine the two?
Something like
$regex = '/(?="|>)http:\/\/www.(.*?)/(.*?)(?"|\<)/';
Sorry not very good at this.
This looks about right to me:
/(?:="|>)http:\/\/www\.(.*?)\/(.*?)["<]/i
Notice a few minor corrections: Your non-capturing group syntax was a little off (it should be (?:pattern)
instead of (?pattern)
), and you also needed to escape the .
and /
.
I'm also not sure the (.*?)\/(.*?)
is doing exactly what you think it is; I'd probably just replace that with (.*?)
unless you want to require a /
character.
Here is a funny thought.
Use /(?:(=")|>)http:\/\/www\.(.*?)\/(.*?)(?(1)"|<)/sg
using a looping find next search. Extracting variables $2 and $3 each time. This uses a conditional.
Or, use /(?|(?<==")http:\/\/www\.(.*?)\/(.*?)(?=")|(?<=>)http:\/\/www\.(.*?)\/(.*?)(?=<))/sg
in a match all. This uses branch reset. The array will acumulate as pairs ($cnt++ % 2).
Depends on what you mean by combining.
A perl test case:
use strict;
use warnings;
my $str = '
<tag asdf="http://www.some.com/directory"/>
<dadr>http://www.adif.com/dir</dadr>
';
while ( $str =~ /(?:(=")|>)http:\/\/www\.(.*?)\/(.*?)(?(1)"|<)/sg )
{
print "'$2' '$3'\n";
}
print "--------------\n";
my @parts = $str =~ /(?|(?<==")http:\/\/www\.(.*?)\/(.*?)(?=")|(?<=>)http:\/\/www\.(.*?)\/(.*?)(?=<))/sg;
my $cnt = 0;
for (@parts)
{
print "'$_' ";
if ($cnt++ % 2) {
print "\n";
}
}
__END__
Output:
'some.com' 'directory'
'adif.com' 'dir'
--------------
'some.com' 'directory'
'adif.com' 'dir'
精彩评论