Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8526151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:11:27+00:00 2026-06-11T08:11:27+00:00

I am trying to write a simple perl script to apply a given regex

  • 0

I am trying to write a simple perl script to apply a given regex to a filename among other things, and I am having trouble passing a regex into the script as an argument.

What I would like to be able to do is somthing like this:

> myscript 's/hi/bye/i' hi.h
bye.h
>

I have produced this code

#!/utils/bin/perl -w
use strict;
use warnings;

my $n_args = $#ARGV + 1;
my $regex =  $ARGV[0];
for(my $i=1; $i<$n_args; $i++) {
  my $file = $ARGV[$i];

  $file =~ $regex;
  print "OUTPUT: $file\n";
}

I cannot use qr because apparently it cannot be used on replacing regexes (although my source for this is a forum post so I’m happy to be proved wrong).

I would rather avoid passing the two parts in as seperate strings and manually doing the regex in the perl script.

Is it possible to pass the regex as an argument like this, and if so what is the best way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:11:29+00:00Added an answer on June 11, 2026 at 8:11 am

    There’s more than one way to do it, I think.

    The Evial Way:

    As you basically send in a regex expression, it can be evaluated to get the result. Like this:

    my @args = ('s/hi/bye/', 'hi.h');
    my ($regex, @filenames) = @args;
    for my $file (@filenames) {
      eval("\$file =~ $regex");
      print "OUTPUT: $file\n";
    }
    

    Of course, following this way will open you to some very nasty surprises. For example, consider passing this set of arguments:

    ...
    my @args = ('s/hi/bye/; print qq{MINE IS AN EVIL LAUGH!\n}', 'hi.h');
    ...
    

    Yes, it will laugh at you most evailly.

    The Safe Way:

    my ($regex_expr, @filenames) = @args;
    my ($substr, $replace) = $regex_expr =~ m#^s/((?:[^/]|\\/)+)/((?:[^/]|\\/)+)/#;
    for my $file (@filenames) {
      $file =~ s/$substr/$replace/;
      print "OUTPUT: $file\n";
    }
    

    As you can see, we parse the expression given to us into two parts, then use these parts to build a full operator. Obviously, this approach is less flexible, but, of course, it’s much more safe.

    The Easiest Way:

    my ($search, $replace, @filenames) = @args;
    for my $file (@filenames) {
      $file =~ s/$search/$replace/;
      print "OUTPUT: $file\n";
    }
    

    Yes, that’s right – no regex parsing at all! What happens here is we decided to take two arguments – ‘search pattern’ and ‘replacement string’ – instead of a single one. Will it make our script less flexible than the previous one? No, as we still had to parse the regex expression more-or-less regularly. But now user clearly understand all the data that is given to a command, which is usually quite an improvement. )

    @args in both examples corresponds to @ARGV array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a simple Perl script that reads a *.csv, places
I'm trying write a simple perl script that reads some fields from a password
So I'm trying to write a perl script to read in a file encoded
I am trying to write a simple IO::Socket connection in perl. However, I am
Well. I'm trying to write simple scheduler in ruby, just simple script which once
I'm trying to write simple client and server C programs, communicating with each other
I'm trying to create a simple cgi perl script that counts down from 10
I'm trying to write the following Perl subroutine. Given are an array a of
I'm trying to write a Perl script which writes to a file and then
I'am trying to write simple script that will get files name from one folder

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.