Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Łukasz Czuliński
8,646 PointsRegular Expressions help: preg_match()
I'm starting to learn Regular Expressions and having some issues.
Basically, I'm trying to parse APA references. For example, with:
Qaseem A, et al. (2008). Current pharmacologic treatment of dementia: A clinical practice guideline from the American College of Physicians and the American Academy of Family Physicians. Annals of Internal Medicine, 148: 370–378.
My current method for extracting the title is:
public static function titleExtract($reference)
{
preg_match('/(\)\.).+?\./', $reference, $match);
return $match[0];
}
But the output is:
). Current pharmacologic treatment of dementia: A clinical practice guideline from the American College of Physicians and the American Academy of Family Physicians.
And I'm trying to get:
Current pharmacologic treatment of dementia: A clinical practice guideline from the American College of Physicians and the American Academy of Family Physicians
I thought by declaring (\)\.) at the beginning and \. at the end, I'm saying, "get everything between those characters, not including them. I've seen a few examples online with similar regexes and the characters in question aren't included in the return. What am I doing wrong?
Łukasz Czuliński
8,646 PointsThanks. Yeah, have been playing around with that. Very useful, but too bad look-behinds aren't supported in JS.
1 Answer
Łukasz Czuliński
8,646 PointsSolved it. Not sure if this is the cleanest method, but used a look-ahead and look-behind.
public static function titleExtract($reference)
{
preg_match('/(?<=\)\.\s)(.+?)(?=\.\s)/', $reference, $match);
return $match[0];
}
thomascawthorn
22,986 Pointsthomascawthorn
22,986 PointsTo help you along, try out this If you're not already.
Very simple but nice gui you can copy and paste code into to see instant response!