Gossamer Forum
Home : General : Perl Programming :

how to do this simple regex?

Quote Reply
how to do this simple regex?
Does anybody know how to return the following string using regex?

Input $var="http://www.mysite.com/toplevel/file.html

Output return "/toplevel/file.html"

I.e. basically how to strip out the absolute path part?
Quote Reply
Re: [scorpioncapital] how to do this simple regex? In reply to
Try;

my $relative;
$var =~ m/^http:\/\/(.+?)\/(.+?)/i and $relative = $2;

That should assign the bit after the first extention to $relative. Untested, but it looks like it should work.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Sep 11, 2003, 12:38 PM
Quote Reply
Re: [Andy] how to do this simple regex? In reply to
weird here is what I got:

on page <%URL%>

INPUT TO GLOBAL:

EDIT BY Andy

regex:

my $relative;
$var =~ m/^http:\/\/(.+?)\/(.+?)/i and $relative = $2;


OUTPUT:

http://eurogirls.net/cgi-bin/V

Last edited by:

Andy: Sep 11, 2003, 12:56 PM
Quote Reply
Re: [scorpioncapital] how to do this simple regex? In reply to
Had to edit out the URL, sorry.

Which global is this with? Wanna PM/email me with details, and I'll have a look for you.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [scorpioncapital] how to do this simple regex? In reply to
Not sure if this has been resolved yet but this regexp will do what you want:

m|^http://.*?(/.*)$|

It basicaly captures everything from the 3rd slash to the end of the URL. This has the benefit of capturing paths which are deeper than one subfolder.

You can add modifiers at the end as needed.

Hope this helps.