Regular Expression Challenge

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I have a problem where I want to parse the days & the forecast data out of a zone forecast, the chunk of text is below.

.TODAY...INCREASING CLOUDINESS. HIGHS IN THE LOWER TO MID 40S.
SOUTH WINDS 10 TO 20 MPH. GUSTS UP TO 30 MPH IN THE AFTERNOON.
.TONIGHT...CHANCE OF RAIN EARLY IN THE EVENING...THEN RAIN IN THE
LATE EVENING AND OVERNIGHT. SLIGHT CHANCE OF THUNDERSTORMS AFTER
MIDNIGHT. TEMPERATURES NEARLY STEADY IN THE MID 30S. SOUTH WINDS
10 TO 15 MPH. CHANCE OF PRECIPITATION 80 PERCENT.
.WEDNESDAY...RAIN IN THE MORNING...THEN RAIN OR SNOW LIKELY IN
THE AFTERNOON BECOMING ALL SNOW. SNOW ACCUMULATION LESS THAN AN INCH.
HIGHS IN THE LOWER 40S. SOUTH WINDS 10 TO 15 MPH EARLY IN THE
MORNING SHIFTING TO THE NORTHWEST 10 TO 20 MPH WITH GUSTS UP TO 30
MPH AT TIMES IN THE LATE MORNING AND AFTERNOON. CHANCE OF
PRECIPITATION 80 PERCENT.
.WEDNESDAY NIGHT...WINDY. SNOW LIKELY. SNOW ACCUMULATION AROUND AN
INCH. LOWS 15 TO 19. NORTHWEST WINDS 20 TO 30 MPH WITH GUSTS UP
TO 35 MPH AT TIMES. CHANCE OF PRECIPITATION 70 PERCENT.
.THURSDAY...WINDY. CLOUDY WITH A 30 PERCENT CHANCE OF SNOW
SHOWERS. HIGHS IN THE MID 20S. NORTHWEST WINDS 20 TO 30 MPH WITH
GUSTS UP TO 35 MPH AT TIMES.
.THURSDAY NIGHT...PARTLY CLOUDY. LOWS 14 TO 18.
.FRIDAY...PARTLY SUNNY. HIGHS IN THE UPPER 20S.
.FRIDAY NIGHT...MOSTLY CLOUDY WITH A 40 PERCENT CHANCE OF SNOW
SHOWERS. LOWS 14 TO 18.
.SATURDAY...MOSTLY CLOUDY WITH A 20 PERCENT CHANCE OF SNOW
SHOWERS. HIGHS IN THE UPPER 20S.
.SATURDAY NIGHT THROUGH SUNDAY NIGHT...PARTLY CLOUDY. LOWS AROUND
20. HIGHS IN THE LOWER 30S.
.MONDAY...MOSTLY SUNNY IN THE MORNING THEN BECOMING PARTLY
CLOUDY. HIGHS IN THE UPPER 30S.

The days were easy to get out with this regular expression:

/\n\.([A-Za-z ])+\.\.\./

...but I am stuck on getting a regular expression to get the forecast data working. If anybody can give me some ideas, I would appreciate it.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I figured it out without using regular expressions at all. Explode the string with "\n." will separate the days, and then explode("...", $data, 2) will split the day title from the forecast for that day.

Sometimes the easiest solution is the best. Smiling

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

If you successfully matched the days, why not do a preg_split with that regex?

EDIT: Or you could do what you just posted Sticking out tongue

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I guess preg_split would have worked as well - didn't know that function existed.

It was one of those things that stumped me, so I left to have some dinner, came back, and the solution hit me right away.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.