开发者

regex multiple parentheses

开发者 https://www.devze.com 2023-02-06 13:43 出处:网络
$str = \"<p>(a)(3) asdf (10) asdf</p>\"; Trying to pull the second set of parentheses using php preg_match_all
$str = "<p>(a)(3) asdf (10) asdf</p>";

Trying to pull the second set of parentheses using php preg_match_all

Very big doc I'm parsing, so currently have this:

preg_match_all("=(?:<p[^>]*>|<p[^>]*>Note|<i>|</i>)\((.*)\)=siU", $str, $matches);

Pulling things like this fine:

<p>(a)
<p>Note(a)
<i>(a)
</i>(a)

All returning (a)

开发者_如何转开发I'd like to also search for any time I see this: <p>(a)(3)

So I need the second paren and its value returned like (3)

And I do not want any other paren + value like the (a) or the (10)


How about something like this?

preg_match_all("=(?:<p[^>]*>(:?Note)?|</?i>)\((.*)\)(?:\((.*)\))?=siU", $str, $matches);


you may be looking for this:

$string = "<p>(a)(3)
<p>Note(ddd)ssssss(a)(3)
<i>dkdkdjkdjkdjkjdkjdk(3)
<i>abc(a)(9)
</i>(a)(3)";


preg_match_all('%<.*?>([\w]+)?(\(.*?\))?(\(3\))%i', $string, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[0]); $i++) {
echo   $result[3][$i]."<br>" ;
}

echo's :

(3)
(3)
(3)
(3)

if you need to match the whole line you may use:

preg_match_all('/(<.*?>([\w]+)?(\(.*?\))?(\(3\)))/i', $string, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[1]); $i++) {
echo   $result[1][$i]."<br>" ; 
}

echo's:

<p>(a)(3)
<p>Note(ddd)ssssss(a)(3)
<i>dkdkdjkdjkdjkjdkjdk(3)
</i>(a)(3)
0

精彩评论

暂无评论...
验证码 换一张
取 消