Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-17-2010, 05:25 PM   #1
Senior Member
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
Perl search and replace

I have a line in a perl script, that replaces urls with an other one:
s@^(http:[^ ]*) @\1 @;

Could it be modified so that instead of simply substituting backreference \1, it substitutes the urlencoded backreference \1?

This is syntactically incorrect for sure, but it may help to make clear what I would like to do:
s@^(http:[^ ]*) @\1) @;
Old 08-17-2010, 09:29 PM   #2
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
well one way you can do it is with a code regex, but that is considered experimental and probably dangerous

s@^(http:[^ ]*)(?{ $http = urlencode($^N) })( .*)@${http}\2@

you can leave out the \2 if you don't need the remainder of the line

perl -e '$var = "BIGLETT and small"; $var =~ s@(.*?)(?{ $lets = lc($^N) })( .*)@${lets}\2@; print "$var\n";'

Last edited by estabroo; 08-17-2010 at 09:31 PM. Reason: example
Old 08-18-2010, 12:02 AM   #3
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
@estabroo I had the same idea as yours. It's just too bad I didn't know how to specify the results using a variable inside the replacement expression. I've tried $1 = uri_escape($^N) but it was a wrong idea since $1 is readonly. I thought that ${var} is only evaluated before the expression starts runtime.

My example now is this:

#!/usr/bin/env perl

use URI::Escape;
use strict;

our $var;

while (<>) {
	s@(http://[^[:blank:]'"]+)(?{ $var = uri_escape($^N); })@${var}@g;
	print $_;
perl < file
Edit: I think this can be easily converted to a one-liner.

Last edited by konsolebox; 08-18-2010 at 12:05 AM.
Old 08-18-2010, 03:51 AM   #4
Senior Member
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Original Poster
Rep: Reputation: 58
Thanks for the codes, I will try them today.

Anyway, the script is going to be an url redirector for squid, so:

a) surplus part of the line (following the space) can be junked (or squid junks it),

b) it need not necessarily be a one-liner. I only try this one-liner, because I have no perl knowledge, and this one-liner is simple and works so far, whilst I remember that the multi-liners I have tried some years ago when I was faced with a similar task, only worked from the command line, but stalled squid for some reason.

The script is going to be part of a simple filtering proxy for my handheld, and its only purpose is to convert/scale down media files in order that they can be viewed on a weaker hardware and lower bandwidth. (However, all urls are passed to the filtering php script, since I want to filter based on response mime-type, rather than request url.)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
perl search and replace nhay Programming 28 10-13-2009 03:46 PM
Perl search and replace issues doublejoon Programming 3 04-24-2008 01:17 PM
Alternative to perl search and replace FirmbIT Programming 2 11-06-2006 09:53 PM
Perl Regexp search-n-replace jpbarto Programming 2 06-16-2005 01:45 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:00 AM.

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