Repeating paterns within paterns

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mstrop
Forum Newbie
Posts: 4
Joined: Thu Nov 09, 2006 7:55 am

Repeating paterns within paterns

Post by mstrop »

Hi,

I have to decode a string, which contains some repeating elements and continues as a generic text. Example:

OCT 28 29 NOV 03 04 05 10 11 DAILY 0800-SS NOV 15 18 23 DAILY SR-SS NOV 24 26 0700-1500 blablabla blabla

output should be an array like

[0][0] = {OCT,28,29,NOV,03,04,05,10,11} // month and days
[0][1] = {0800,SS} // start and end data - SS = sunset, SR = sunrise
[1][0] = {NOV,15,18,23}
[1][1] = {SR,SS}
[2][0] = {NOV,24,26}
[2]10] = {0700,1500}

I am not able to construct such a patern. Please, could anybody help me?

M.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Code: Select all

preg_match_all('/

# month and days
(
 (?:
  [A-Y]{3}\s
  (?:\d{2}\s)+
 )+
)

(?:DAILY\s)?

# sunrise and sunset
(
 (?:\d{4}|SR)
 -
 (?:\d{4}|SS)
)

/x',

'OCT 28 29 NOV 03 04 05 10 11 DAILY 0800-SS NOV 15 18 23 DAILY SR-SS NOV 24 26 0700-1500',

$matches);

Code: Select all

// contents of $matches
// combine [1][0] with [2][0], [1][1] with [2][1], etc

Array
(
    [0] => Array
        (
            [0] => OCT 28 29 NOV 03 04 05 10 11 DAILY 0800-SS
            [1] => NOV 15 18 23 DAILY SR-SS
            [2] => NOV 24 26 0700-1500
        )

    [1] => Array
        (
            [0] => OCT 28 29 NOV 03 04 05 10 11 
            [1] => NOV 15 18 23 
            [2] => NOV 24 26 
        )

    [2] => Array
        (
            [0] => 0800-SS
            [1] => SR-SS
            [2] => 0700-1500
        )

)
mstrop
Forum Newbie
Posts: 4
Joined: Thu Nov 09, 2006 7:55 am

Post by mstrop »

Thanks a lot !!!!

M.
Post Reply