CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Substr and regex

 



perlkid
stranger

May 25, 2000, 6:02 PM

Post #1 of 9 (4163 views)
Substr and regex Can't Post

 
I'm trying to check if any unwanted characters are getting in before urls in fields. I thought that I could substr the first four characters in a field and if they are not equal to http then the script would then capture everything before the url and then subtitute it using s///;

I know this must have somthing to do with taking the information from the first character until http and substituting it.

I hope I explained that ok.

perlkid


Peter Van Hoecke
Deleted

May 26, 2000, 1:06 AM

Post #2 of 9 (4163 views)
Re: Substr and regex [In reply to] Can't Post

If I didn't misunderstand the question, this could be a possible solution

anyway...

#!perl.exe
use strict;
my( $wrongUrl );
$wrongUrl = "rubbish http://www.perlguru.com";
print "This is a possible URL => $wrongUrl\n";
$wrongUrl =~ s/^.*(http:\/\/)/$1/;
print "This is the good version => $wrongUrl \n";

I know the regex can be made more beautiful, but it does the trick...


perlkid
stranger

May 26, 2000, 3:07 AM

Post #3 of 9 (4163 views)
Re: Substr and regex [In reply to] Can't Post

 
Thanks Peter,

You knew exactly what I ment. So do I have to use strict?

Could you explain that line that does all the work so that I can Understand how it works to use it in the future for other things Smile

This line;

$wrongUrl =~ s/^.*(http:\/\/)/$1/;

Thanks A lot.

Especialy For The Quick Reply.

perlkid


Peter Van Hoecke
Deleted

May 26, 2000, 3:48 AM

Post #4 of 9 (4163 views)
Re: Substr and regex [In reply to] Can't Post

The line "use strict" forces Perl to complain when you use uninitialyzed values. Perl then generates warnings to all suspicious coding... With use strict, you HAVE to declare the variables in a "my($variable)" declaration. If you make a typo, Perl will mention it to you. This prevents a lot of problems, but is not obligatory.

Now about the regex...
You want to search after stuff that precedes "http://", and replace these letters by nothing. In other words, you want to find a string ending with "http://" and replace the string perl found with "http://" alone.
$1 represents the first of embraced patterns... the (http:\/\/)

So...
s/ # substitute
^ # the start of a string, followed by
.* # 0 or more letters, followd by
(http:\/\/) # http://, but with the / escaped by using \
/ # substitute by
$1 # the stuff between the first braces
/;



perlkid
stranger

May 26, 2000, 4:06 PM

Post #5 of 9 (4163 views)
Re: Substr and regex [In reply to] Can't Post

 
Thank You Peter,

I definatly understand and I'll be using code like that for a lot of other stuff too.

Thanks,

perlkid


Cure
User

May 26, 2000, 5:00 PM

Post #6 of 9 (4163 views)
Re: Substr and regex [In reply to] Can't Post

Hi Perlkid


\ == Quote the next metacharacter
^ == Match the beginning of line
. == Match any character (except newline)
$ == Match the end of line or (before newline at the end)
() == grouping
[] == character class

######################
Standard Quantifiers
* == Match 0 or more times
+ == Match 1 or more times
? == Match 1 or 0 times
{n} == Match exaclty n times
{n,} == Match atleast n times
{n,m} == Match atleast n times but no more than m times


Cure




Cure
User

May 26, 2000, 6:47 PM

Post #7 of 9 (4163 views)
Re: Substr and regex [In reply to] Can't Post

$wrongUrl =~ s!^.*(http://)!$1!;



japhy
Enthusiast / Moderator

May 26, 2000, 7:09 PM

Post #8 of 9 (4163 views)
Re: Substr and regex [In reply to] Can't Post

Offering you Yet Another Way To Do It...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$url =~ s/.*?(?=http)//;
# remove as little as possible before an "http"

$url =~ s/(? Frown?!http).)*//;
# remove as much as possible that is NOT the string "http"
# is technically the same as
# s/.*?(http)/$1/
</pre><HR></BLOCKQUOTE>


perlkid
stranger

May 27, 2000, 12:48 AM

Post #9 of 9 (4163 views)
Re: Substr and regex [In reply to] Can't Post

 
Thanks Cure and Japhy,

By looking at cure's definitions and trying to understand the syntax in japhy's code, I think I'm getting the hang of this. Thanks A Whole Lot Guys.

perlkid

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives