Joe
Joe

Reputation: 26787

Help with regex to extract value from URL anchor

In Django URL patterns, what's the proper way to take this URL:

http://example/i/fb/#access_token=12345678910|b1827b912847b81938747b91849-193867192305817|EKWOGJhcinWIjWij8174-NgjRojb&expires_in=0

And create variables:

access_token = 12345678910|b1827b912847b81938747b91849-193867192305817|EKWOGJhcinWIjWij8174-NgjRojb

expires_in = 0

I've been trying to get this to work, but to no avail...

url(r'^fb/(?P([^&]+)(?:&expires=(.*))?/?$, 'app.views.test_url', name="test_url"),

Upvotes: 0

Views: 245

Answers (1)

Ruel
Ruel

Reputation: 15780

Try this:

'[#?&]?([a-z_]+)=([0-9A-Za-z|\-]+)'

Example application in PHP:

<?php

    $url = "http://example/i/fb/#access_token=12345678910|b1827b912847b81938747b91849-193867192305817|EKWOGJhcinWIjWij8174-NgjRojb&expires_in=0";
    preg_match_all("%[#?&]?([a-z_]+)=([0-9A-Za-z|\-]+)%", $url, $matches, PREG_SET_ORDER);

    print_r($matches);

?>

Upvotes: 1

Related Questions