I’m still learning PHP regular expressions (and regular expressions in general), and would like to ask for some help in returning a match from a non-greedy match on filler (I am working on an open-source CalDav client called Calico, that should be released to the community soon).
I used http://txt2re.com to generate this regular expression, and I am having trouble understanding how to retrieve whatever ‘$re4=’.*?’; # Non-greedy match on filler’ manages to catch. Could someone please help me?
$txt='BEGIN:VCALENDAR PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN VERSION:2.0 BEGIN:VTIMEZONE TZID:America/New_York X-LIC-LOCATION:America/New_York BEGIN:DAYLIGHT TZOFFSETFROM:-0500 TZOFFSETTO:-0400 TZNAME:EDT DTSTART:19700308T020000 RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3 END:DAYLIGHT BEGIN:STANDARD TZOFFSETFROM:-0400 TZOFFSETTO:-0500 TZNAME:EST DTSTART:19701101T020000 RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11 END:STANDARD END:VTIMEZONE BEGIN:VEVENT CREATED:20111219T194407Z LAST-MODIFIED:20091219T197731Z DTSTAMP:20111219T194431Z UID:c9bfc6b0-064e-4316-83fe-753db34e67ee SUMMARY:New Test DTSTART;TZID=America/New_York:20111219T150000 DTEND;TZID=America/New_York:20111219T160000 LOCATION:Philadelphia DESCRIPTION:Propfind test. END:VEVENT END:VCALENDAR ';
$re1='.*?'; # Non-greedy match on filler
$re2='(TZID)'; # Word 1
$re3='(:)'; # Any Single Character 1
$re4='.*?'; # Non-greedy match on filler
$re5='( )'; # Any Single Character 2
if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5."/is", $txt, $matches))
{
$test = $matches[0][0];
$word1=$matches[1][0];
$c1=$matches[2][0];
$c2=$matches[3][0];
print "($test) ($word1) ($c1) ($c2) \n";
}
The text I am interested in is ‘TZID:America/New_York’, of which the ‘America/New_York’ portion I am trying to grab. Since the TZID can ultimately be anything (possibility of non-standard timezones), I am just trying to grab everything from the colon to the whitespace.
You don’t have the
$re4part wrapped in capturing parentheses so the match won’t actually be getting saved in to$matches.If you change it to
$re4='(.*?)';then$matches[3][0]will contain ‘America/New_York’If you’re just trying to capture the timezone you could use a simpler pattern: