ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The idea is simple, but I'm not sure the implementation is.
Anyway, as part of my program, I want the user to be able to specify a regex, like:
Enter a string: ... blah blah ...
Enter a regex: s/blah/yawn/g
I've come that far, in fact. I can also use regex stuff on the left side, i.e. s/\b\w{5}\b/yawn/g.
However, I can't get backreferences to work. What'd be the ultimate thing is if the user could enter something like
s/blah (\w+) blah/$1/g
Is this possible? I've tried a bit with qr// and eval, but no luck. I'm pretty new to Perl, btw. So far, all it does is print the literal '$1' instead of the value I want.
In general, this may be a security risk. To bypass security checking, put this at the beginning of your source file:
Code:
use re 'eval';
Hmm, thanks, but unless I'm missing something (I hope so), it doesn't help.
Here's the (testing) code, and a sample run:
Code:
use strict;
use warnings;
use re 'eval';
print "Enter a regex: ";
chomp(my $regex = <STDIN>);
print "Enter a replacement: ";
chomp(my $repl = <STDIN>);
print "Regex: $regex\n";
print "Repl : $repl\n";
my $str = "A bunch of words to search and replace on";
print "Old: $str\n";
$str =~ s/$regex/$repl/g;
print "New: $str\n";
Quote:
Enter a regex: \b(\w{5})\b
Enter a replacement: >>$1<<
Regex: \b(\w{5})\b
Repl : >>$1<<
Old: A bunch of words to search and replace on
New: A >>$1<< of >>$1<< to search and replace on
It seems I had to split the regex for it to work. (To minimize bugs, I removed my split code and made it two lines for the test.)
my $regex = pop @ARGV;
my ($search, $repl, $flags) = ($1, $2, $3) if $regex =~ m{^s/(.*?)/(.*?)/(\w+)?};
$repl = "" unless defined $repl;
$flags = "" unless defined $flags;
die "Unable to read regex! ...blah...\n" unless (defined $search && defined $repl);
eval '$newname =~ s/$search/' . "$repl/$flags";
die "Unable to execute substitution! Invalid regular expression?\nError message was: $@\n" if $@;
Works great!
I'm writing a mass-rename util. Fully functional by now!
Is there an app like this out there? In any case I feel good at the moment, this will be used a lot by myself!
BTW, with the regex checker in the code above, is security still a concern?
I don't see how it'd matter, though, re 'eval' is off, and even if it was exploitable it wouldn't really matter; it's not like it's going to be setuid root or anything. If you can run perl -e 'eval "unlink $_ for <*>"' you can run rm -f * too...?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.