RegEx Help - Not parsing like it should

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

Moderator: General Moderators

Post Reply
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

RegEx Help - Not parsing like it should

Post by aliasxneo »

Here is my code:

Code: Select all

private function parse_cookies()
    {
        $lines = split($this->newline, $this->header);

        foreach ($lines as $line)
        {
            if (preg_match("/Set-Cookie\: (.*)=(.*)\;/", $line, $matches))
            {
                $this->cookies[$matches[1]] = $matches[2];
            }
        }
    }
Basically I am parsing cookies from an HTTP header. It works pretty good except that the RegEx is parsing more than it should. Here is the output when printing the cookies array:
neodebug=deleted; expires=Wed, 18-Jan-06 04:33:13 GMT; path = /
nupi=0; expires=Thu, 18-Jan-07 02:53:14 GMT; path = /
nupid=0; expires=Thu, 18-Jan-07 02:53:14 GMT; path = /
npid=0; expires=Thu, 18-Jan-07 02:53:14 GMT; path = /
np_uniq=pending; expires=Fri, 18-Jan-08 04:33:14 GMT; path = /
xt6Yr4e33D=59182809479467180131245; expires=Fri, 18-Jan-08 04:33:14 GMT; path = /
Basically what it's doing is skipping the ";" and grabbing the rest of the line. Take the first line for example, it's "neodebug=deleted; expires", as you can see there is a ";" there but in my RegEx I wanted it to stop at the ";" and only grab the things before it but as seen here it's just completely skipping it. Is there a reason for this? Thanks.

Cheers,
- Josh
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

preg_match runs in greedy mode by default. (.*) tries to match as much characters as possible.
try

Code: Select all

/Set-Cookie: ([^=]+)=([^;]+);/
or

Code: Select all

/Set-Cookie: (.+)=(.+);/U
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

um.... just checking but you could use $_COOKIE if you're dealing with current http headers.

also, you can modify the greediness with a trailing ? like so:

Code: Select all

/Set-Cookie: (.+?)=(.+?);/
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Kireran's code should work. As you can see you don't need to escape the semi-colon or colon.
Post Reply