razta Posted April 19, 2010 Share Posted April 19, 2010 Hello, I am trying to match a regex and extract the data from the matched string that I want. I want to extract the Apache version only from the Apache website. So I am using the following raw sting to do the match: <li><a href="#apache22">2.2.15</a> (released 2010-03-06)</li> And the data I want from the above raw string is just the '2.2.15'. Here is the code related to this problem: // Match regex in grabbed HTML source preg_match('/<li><a\shref="#apache22">(\d\.\d\.\d?\d)<\/a>\s\(released\s\d\d\d\d-\d\d-\d\d\)<\/li>/', $grabPage, $regex_version); echo $regex_version[0]; I expected the above to output '2.2.15' instead it output '<li><a href="#apache22">2.2.15</a> (released 2010-03-06)</li>'. Any help appreciated. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/199003-php-regex-grouping/ Share on other sites More sharing options...
cags Posted April 19, 2010 Share Posted April 19, 2010 Capture group 0 always returns whatever string matched the entire pattern, therefore that is what is located in $regex_version[0]. The version number 2.2.15 should be contained within $regex_version[1]. Link to comment https://forums.phpfreaks.com/topic/199003-php-regex-grouping/#findComment-1044562 Share on other sites More sharing options...
razta Posted April 19, 2010 Author Share Posted April 19, 2010 Something so simple yet fundamental. Thank you very much for your help! Link to comment https://forums.phpfreaks.com/topic/199003-php-regex-grouping/#findComment-1044567 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.