Your problem description reveals that you're looking to capture the string between double quotes
when they're directly after title
and between the braces of @article
. Therefore, you can use this regex:
/@article{[^}]+title.*?\"([^"]*)\"/
Regex Explanation:
@article{
Matches an open @article
string, along with the bracket.
[^}]+
Matches any amount of non-"}"
s, effectively implying that our match is within the braces, and not outside the tag.
\"([^"]*)\"
Matches a pair of double quotes, captures everything in them.
However, here's an optimized and more accurate regex just for you:
'/@article{(?>[^}t]+|t(?!itle\h*=))+title\h*=.*?\"(.*?)\"(?![^"\n]")/'
So you have:
$file = file("master.bib");
$m = array();
preg_match_all('/@article{(?>[^}t]+|t(?!itle\h*=))+title\h*=.*?\"(.*?)\"(?![^"\n]")/', $file, $m);
echo $m[1];
echo "<br>";</br>
Here's a regex demo.