Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > problems with Perl RegEx match

Reply
Thread Tools

problems with Perl RegEx match

 
 
mitch
Guest
Posts: n/a
 
      12-28-2004
Hi folks,

I am working on a what I thought would be fairly simple regular
expression search. I am simply trying to take a full URL (something
like http://www.someurl.com/someDir/file.html) and parse it out into
its components domain and path (domain = www.someurl.com & path =
/someDir/file.html). So I wrote this little script to do just that.
However, I am getting an error when I run my script and I think it has
something to do with my regex function that I am using. So here is the
code snippet of the offending line:

if ($externalLink =~ m/http:\/\/([^\/]+)/) {
$domain = $1;
$path = $';
}

The error that I am getting is this:
Missing right bracket at ./LogPreProcessor_Inetprod.pl line 64, at end
of line
syntax error at ./LogPreProcessor_Inetprod.pl line 64, at EOF
Execution of ./LogPreProcessor_Inetprod.pl aborted due to compilation
errors.

I also tried to modify the script by removing the \ from within the
square brackets:

if ($externalLink =~ m/http:\/\/([^/]+)/) {
$domain = $1;
$path = $';
}

But that too gives me an error:
bash-2.03$ LogPreProcessor_Inetprod.pl inet.log
/http://([^/: unmatched [] in regexp at ./LogPreProcessor_Inetprod.pl
line 26.
bash-2.03$

So I tried a bunch of other variations, wich also did not work. After
consulting several books and googling for some solution, I thought that
I had exhausted all my research possibilities and I thought that the
smart folks in this group could show me the error of my ways.

On a side note, when I used the following search term in google to look
for a solution:

Perl "[^/]"

Google seemed to ignore all those special characters, and simply
returned any page that contained the term 'Perl'. So that was useless.
Thanks for all your help in this matter.

Regards,

Mitch

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-28-2004
mitch wrote:
> I am working on a what I thought would be fairly simple regular
> expression search. I am simply trying to take a full URL (something
> like http://www.someurl.com/someDir/file.html) and parse it out into
> its components domain and path (domain = www.someurl.com & path =
> /someDir/file.html). So I wrote this little script to do just that.
> However, I am getting an error when I run my script and I think it
> has something to do with my regex function that I am using. So here
> is the code snippet of the offending line:
>
> if ($externalLink =~ m/http:\/\/([^\/]+)/) {
> $domain = $1;
> $path = $';
> }


That code works fine for me.

> The error that I am getting is this:
> Missing right bracket at ./LogPreProcessor_Inetprod.pl line 64, at
> end of line syntax error at ./LogPreProcessor_Inetprod.pl line 64, at
> EOF Execution of ./LogPreProcessor_Inetprod.pl aborted due to
> compilation errors.


So, what's at (and right before and after) line 64 in your script?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Lars Eighner
Guest
Posts: n/a
 
      12-28-2004
In our last episode,
< .com>, the
lovely and talented mitch broadcast on comp.lang.perl.misc:

> Hi folks,


> I am working on a what I thought would be fairly simple regular
> expression search. I am simply trying to take a full URL (something
> like http://www.someurl.com/someDir/file.html) and parse it out into
> its components domain and path (domain = www.someurl.com & path =
> /someDir/file.html). So I wrote this little script to do just that.
> However, I am getting an error when I run my script and I think it has
> something to do with my regex function that I am using. So here is the
> code snippet of the offending line:


> if ($externalLink =~ m/http:\/\/([^\/]+)/) {
> $domain = $1;
> $path = $';
> }


I'm not following all of this but, why make things hard on
yourself. Use any other quote character besides /, and you
won't have to escape them. Also as you would see, if you even
took a glance at perlretoot, / is NOT special in [].

m#http://([^/]+)#

isn't that about 100 times easier to read and work with?

(However, even as it stands, your snippet works fine for
me.)

> The error that I am getting is this:
> Missing right bracket at ./LogPreProcessor_Inetprod.pl line 64, at end
> of line
> syntax error at ./LogPreProcessor_Inetprod.pl line 64, at EOF
> Execution of ./LogPreProcessor_Inetprod.pl aborted due to compilation
> errors.


Useless without all of the code. Pretty likely the problem is
above the snippet.

You know there is a module to parse URLs. So, howcome reinvent
the wheel?

> I also tried to modify the script by removing the \ from within the
> square brackets:


> if ($externalLink =~ m/http:\/\/([^/]+)/) {
> $domain = $1;
> $path = $';
> }


> But that too gives me an error:
> bash-2.03$ LogPreProcessor_Inetprod.pl inet.log
> /http://([^/: unmatched [] in regexp at ./LogPreProcessor_Inetprod.pl
> line 26.
> bash-2.03$


Yeah, that really doesn't work, even without garbage above it,
but nothing will work because you did not include in the
original snip the part with the real problem.

> So I tried a bunch of other variations, wich also did not work. After
> consulting several books and googling for some solution, I thought that
> I had exhausted all my research possibilities and I thought that the
> smart folks in this group could show me the error of my ways.


> On a side note, when I used the following search term in google to look
> for a solution:


> Perl "[^/]"


> Google seemed to ignore all those special characters, and simply
> returned any page that contained the term 'Perl'. So that was useless.
> Thanks for all your help in this matter.


> Regards,


> Mitch


--
Lars Eighner http://www.io.com/~eighner/
Behaviorism is the art of pulling habits out of rats. -- O'Neill
 
Reply With Quote
 
mitch
Guest
Posts: n/a
 
      12-28-2004
Hi everyone,

Thanks first of all for all the great comments and suggestions. So
Abigail, you were right, I foolishly forgot to close a silly brace.
That should teach me to write code after only sleeping for two hours
(boy do I feel sheepish).

In terms of indenting, my code is beautifully indented, however while
posting it, the tabs and spaces had been removed. Next time I'll add
some HTML code to the posting, just so that it gets formatted properly.


In any event, I appreciate all the comments and thanks again for the
prompt response. It took me literaly 10 seconds to find the missing
brace. I guess sometimes you have been staring too long at the code to
see the problem. I was convinced that the problem was in the RegEx.
Well, live and learn.... thanks again folks.

Regards,

Mitch

 
Reply With Quote
 
Peter Wyzl
Guest
Posts: n/a
 
      12-28-2004
"mitch" <> wrote in message
news: oups.com...
: Hi everyone,
:
: Thanks first of all for all the great comments and suggestions. So
: Abigail, you were right, I foolishly forgot to close a silly brace.
: That should teach me to write code after only sleeping for two hours
: (boy do I feel sheepish).
:
: In terms of indenting, my code is beautifully indented, however while
: posting it, the tabs and spaces had been removed. Next time I'll add
: some HTML code to the posting, just so that it gets formatted properly.

Don't do that. Just replace the tabs with an appropriate number of spaces
(3 or 4 should be fine)

--
Wyzelli


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Match protocol http - problems in regex Rob Cisco 2 08-02-2010 05:46 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
How to make Perl's regex engine "halt" after a match Dominic van der Zypen Perl Misc 14 11-18-2006 12:02 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57