LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2004, 08:14 AM   #1
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Rep: Reputation: 30
URL parsing program in perl


Hi all,
Also got this problem, need to build a program to parse a URL.
I have only got so far, it can parse a whole URL, but not if there are parts missing, it doesn't like that for some reason, any ideas???

Code:
#!/usr/bin/perl -w
use strict;

# for parsing URL's like:
# "http://www.linuxquestions.org:8080/forums/search.cgi?string=help#me"

# will parse if ALL parts are there, but the problem is getting the input into the array
# when not all parts are there.

# declare variables

my ($proto, $host, $port, $path, $query, $frag);
$proto    = '(\w*)';
$host     = '([\w\.]*)';
$port     = ':(\d*)';
$path     = '([\w/\.]*)';
$query    = '\?([\w=]*)';
$frag     = '#(\w*)';


print ("\nEnter a URL to parse\n");

while(<STDIN>){
    chomp;
    my $url = "${proto}://${host}${port}?${path}${query}?${frag}?";
    my @answers =  m|$url|;

    # protocol and host are compulsory
    print("Protocol = $answers[0]\n");
    print("Host = $answers[1]\n");

    if(defined($answers[2])){ print("Port = $answers[2]\n"); }
    if(defined($answers[3])){ print("Path = $answers[3]\n"); }
    if(defined($answers[4])){ print("Query = $answers[4]\n"); }
    if(defined($answers[5])){ print("Fragment  = $answers[5]\n"); }
}
Any ideas??? Please?!?!?!
 
Old 04-01-2004, 10:43 PM   #2
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Original Poster
Rep: Reputation: 30
Anyone got any ideas???
 
Old 09-30-2010, 03:57 PM   #3
helamonster
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
A Working example...

The following could use some improvement, but works:

Code:

if ( $url =~ m/^(\w+:\/\/)?(\w*)?(:\w*)?@?([\w.]+)*(:\d*)?(\/\w*)?$/ )
{

	if(defined($1))
	{
		$db_proto = $1;
	}
	
	if(defined($2))
	{
		$db_user = $2;
	}
	
	if(defined($3))
	{
		$3 =~ m/^:(\w+)$/;
		$db_pass = $1;
	}
	
	if(defined($4))
	{
		$db_host = $4;
	}
	
	if(defined($5))
	{
		$5 =~ m/^:(\d+)$/;
		$db_port = $1;
	}
	
	if(defined($6))
	{
		$6 =~ m/^\/(\w+)$/;
		$db_name = $1;
	}
 
Old 09-30-2010, 03:58 PM   #4
helamonster
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
You could also use the APR::URI module (http://perl.apache.org/docs/2.0/api/APR/URI.html)

FYI: My example was not checking for #frag -- I was using URLs like the following:
Code:
http://user:password@server:1234/database
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing URL in PHP benrose111488 Programming 6 10-12-2005 12:12 PM
Mozilla Linux Command Line URL Parsing Security Flaw Reported win32sux Linux - Security 9 10-06-2005 06:39 AM
Parsing HTML using Perl smaida Programming 2 05-29-2004 01:20 PM
parsing a logfile in perl ludeKing Programming 2 04-30-2004 03:20 AM
auto url parsing on console bluecadet Linux - General 1 12-18-2001 04:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration