Link to home
Start Free TrialLog in
Avatar of tilmes
tilmesFlag for Germany

asked on

stip out URL in detail view


I need to remove URL which begins with http or www in the field $fields[12].

Below is a only example to remove email address.
I need for above in same principle.
$fields[12] =~ s/[a-zA-Z0-9\-]+@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}/ /;
Avatar of BioI
BioI
Flag of Belgium image

next command removes www. ["dot" after www included] or http:// [: en // included] or http://www]

$fields[12]=~ s/^[www\.|http:\/\/|http:\/\/www\.]+//;

Avatar of gourav_jain
gourav_jain

Hi Tilmes
I hope this will work for you....

$fields[12] =~ s/^http|www.*//g;

Gourav Jain
Avatar of tilmes

ASKER

Hello Bio,

It does not work and furthermore
i need to remove whole URL inc. www or http
Avatar of tilmes

ASKER

Hello gourav_jain
the URL e.g.
http://webspace.abc.at
does not work.
Avatar of tilmes

ASKER

I added with
$fields[12] =~ s/^|http:\/\/.*|www.*//g;
and it removes all the text behind http or www.
Can limit to only URL?
ASKER CERTIFIED SOLUTION
Avatar of BioI
BioI
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FishMonger
BioI's first method should have worked, but here's a condensed version that also works.

$fields[12] =~ s/^(?=www|http))\S+//i;
If you want to be a little more precise:

$fields[12] =~ s!^(?=www\.|http://))\S+!!i;
Avatar of tilmes

ASKER

I don't know why but those are not working
the solution of FishMonger also doesn't work?
what sort of output do you get when you use these substitution syntaxes?
can you give some examples of url's and the output using the above "solutions"...
Avatar of tilmes

ASKER

I had a some kind of error with $fields[12] =~ s/^(?=www|http))\S+//i;
And what really worked is still
$fields[12] =~ s/^|http:\/\/.*|www.*//g;
Your regex says to match NOTHING at the beginning of the string OR http:// OR www. anywhere in the string.  So, if yours works and ours doesn't, it's because you have something other than www or http at the beginning of the string.  You could remove the beginning of string line anchor.

Here's a test script that demistrates this; just uncomment the regex that you want to test

@fields = (
           'https://www.experts-exchange.com/ test1 test2',
           'oldlook.experts-exchange.com/ test3 test4',
           'https://www.experts-exchange.com/ test5 test6',
           "\t\thttps://www.experts-exchange.com/ test7 test8",
          );

for $i (0..$#fields) {
#   $fields[$i] =~ s/^[www\.|http:\/\/|http:\/\/www\.]+//; # BioI's first regex
#   $fields[$i] =~ s/^[www\.|http:\/\/|http:\/\/www\.]+\S+//; # BioI's second regex
#   $fields[$i] =~ s/^(http|www).*//g;  # gourav_jain's corrected regex
#   $fields[$i] =~ s/^(?=www|http)\S+//i; # FishMonger's regex
#   $fields[$i] =~ s/^|http:\/\/.*|www.*//g; # your regex
   print "$fields[$i]\n";
}

BioI and I both thought you wanted to remove just the url, but if you want the entire string removed, we need to modify the end of the regex or use splice to delete the array element.
Take note of the second array element.  That's a valid url (not counting the test test).  Do you want to be able to match and delete those types of urls?
Avatar of tilmes

ASKER

Hello FishMonger,

i appreciate that you wrote very detail. i can not answer to your quetion.
and copyed the script i have below.

for ($i = 0;$i <= 21;$i++) {
      $fields[$i] =~ s/~p~/\|/g;
      $fields[$i] =~ s/~nl~/<br>/g;
        if ($fields[$i] eq "") { $fields[$i] = "&#151;"; }
}

$fields[18] =~ s/&#151;//g;
# strip most email addresses.
$fields[12] =~ s/[a-zA-Z0-9\-]+@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}/ /;

# strip the number beginning w/ a 0.
$fields[12] =~ s/(^|\D)0+\d*\D{0,1}\d*/$1/g;
$fields[12] =~ s/^|http:\/\/.*|www.*//g;
I'm not sure what you're asking me, but will this help?

These three regex's do the exact same thing.

$fields[12] =~ s/^|http:\/\/.*|www.*//g;
$fields[12] =~ s/http:\/\/|www.*//g;
$fields[12] =~ s!http://|www.*!!g;

Notice that on the 2nd & 3rd regex I've removed ^| and on the 3rd regex I used a different delimiter so that the forward slashes don't need to be escaped.

The initialization of the for loop can be made more readable if you change it to:

for $i (0..21)

I'd need to look at more of your script to be sure, but I think it might be better to use a hash instead of the array.
and maybe explain what you want to do with the url:
- do you want to delete the complete line when there is a url inside? or do you only want to delete the url?
- does the www or http have to appear in the beginning of your line or everywhere in the line?

p.s. indeed, using a hash sounds good...
tilmes,

There seems to a fair amount of obfuscation going on in the snippets of your scripts (in each of your questions) that you've posted.  It might be helpful (for you and us) if you give us a more complete explaination of your project.  We try to do the best we can to answer your questions, but it seams that once you put these fragments together, your script gets more obfuscated.