spiritway has asked for the wisdom of the Perl Monks concerning the following question:

Beloved Brethren and Sistren:

I am trying to do what should be a simple substitution, replacing text such as 'http://www.example.com' with a single space. I would have thought that this would do it: $file =~s/http\S+/ /ig;. The goal is to detect 'http' followed by one or more non-whitespace characters. Alas, the substitution doesn't occur, and the unwanted text remains untouched. What am I doing wrong? I seem to be running into a dead end here.

Update: OK, an example of the input follows:

Why pay big bucks? Create your OWN website now! All best software! New software on our site: Illustrator CS - $69.95 # ... Many more lines of this stuff... Visio 2003 Professional - $69.95 Our site: http://polardd.com

This is the input text (one of several files). It is also the output text. I view this using Microsoft Wordpad. I am comforted to find that my code isn't completely off... but of course, I still haven't figured out what the problem actually is... Thanks for the responses, though.

Update 2: I may be a contender for the OT: most egregious programming error, ever award. I was trying to do the substitution on the file name, not the text within the file. I hang my head in shame and will do penance.

Replies are listed 'Best First'.
Re: s/// to replace HTTP address with a space
by duff (Parson) on Feb 22, 2006 at 05:55 UTC

    Without knowing the contents of $file or how you're checking that the "text remains untouched" I don't think that anyone can tell you what you're doing wrong with any certainty. But just to show you that your code does indeed "work":

    #!/usr/bin/perl use strict; use warnings; my $str = "This is a string with a URL like http://www.example.com in +it"; print "$str\n"; $str =~ s/http\S+/ /ig; print "$str\n"; __END__ This is a string with a URL like http://www.example.com in it This is a string with a URL like in it

    Perhaps it would help you to try to reduce the problem to something simple like the above?

Re: s/// to replace HTTP address with a space
by friedo (Prior) on Feb 22, 2006 at 05:55 UTC
    Can you show some sample data? Your pattern works fine for me.

    perl -le '$foo = "blah blah http://www.foo.com/ blah blah"; $foo =~ s/ +http\S+/ /ig; print $foo' blah blah blah blah
Re: s/// to replace HTTP address with a space
by strat (Canon) on Feb 22, 2006 at 09:08 UTC

    One of the URI::Find modules can be interesting for you

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: s/// to replace HTTP address with a space
by davidrw (Prior) on Feb 22, 2006 at 14:10 UTC