开发者

Getting a specific index in Regular Expression Ex: (test 1 test 2 test 3) I would like to match the second 'test'

开发者 https://www.devze.com 2023-04-06 16:50 出处:网络
Getting a specific index in Regular Expression Ex: (test 1 test 2 test 3) I would like to match the second \'test\',

Getting a specific index in Regular Expression Ex: (test 1 test 2 test 3) I would like to match the second 'test', I'm using PHP preg_match

I tried to use preg_match_all but that is a slow function and I need high performance on this.

for example I have a HTML

I would like to match the third match

$str = "<span>1</span><span>2</span><span>3</span>";
preg_match('/<span>(\d+)<\/span>/',$str,$matches);
print_r($matches);

Something like: '/(\d+)</span>[2]/'

Thank you guys ver开发者_StackOverflow中文版y much.


preg_match_all is your best bet less xml parsing. This will also work, but is very ugly:

/(?:<span.+?span>){2}<span>(\d+)<\/span>/


function regbuild($num) {
    return "/(?:<span.+?span>){". --$num ."}<span>(\d+)<\/span>/"
}
0

精彩评论

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