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 3324064
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:22:37+00:00 2026-05-17T23:22:37+00:00

Can anyone explain regular expression text substitutions when the regular expression is held in

  • 0

Can anyone explain regular expression text substitutions when the regular expression is held in a variable? I’m trying to process some text, Clearcase config specs actually, and substitute text as I go. The rules for the substitution are held in an array of hashes that have the regular expression to match and the text to substitute.

The input text looks somthing like this:

element  /my_elem/releases/...  VERSION_STRING.020 -nocheckout

Most of the substitutions are simply to remove lines that contain a specific text string, this works fine. In some cases I want to substitute the text, but re-use the VERSION_STRING text. I’ve tried using $1 in the substitution expression but it doesn’t work. $1 gets the version string in the match, but the replacement of $1 doesn’t work in the substitution.

In these cases the output should look something like this:

element  -directory  /my_elem/releases/... VERSION_STRING.020 -nocheckout
element  /my_elem/releases/.../*.[ch]  VERSION_STRING.020 -nocheckout

ie. One line input became two output and the version string has been re-used.

The code looks something like this. First the regular expressions and substitutions:

my @Special_Regex = (   
                  { regex => "\\s*element\\s*\/my_elem_removed\\s*\/main\/\\d+\$",                  subs => "# Line removed" },
                  { regex => "\\s*element\\s*\/my_elem_changed\/releases\/\.\.\.\\s*\(\.\*\$\)", 
                    subs => "element  \-directory  \/my_elem\/releases\/\.\.\. \\1\nelement  \/my_elem\/releases\/\.\.\.\/\*\.\[ch\]  \\1" }

                );

In the second regex the variable $1 is defined in the portion (.*\$) and this is working correctly. The subs expression does not substitute it, however.

 foreach my $line (<INFILE>)
        {
        chomp($line);
        my $test = $line;
        foreach my $hash (@Special_Regex)
        {
            my $regex = qr/$hash->{regex}/is;
            if($test =~ s/$regex/$hash->{subs}/)
                {
                print "$test\n";
                print "$line\n";
                print "$1\n";
                }
         }
}

What am I missing? Thanks in advance.

  • 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-05-17T23:22:38+00:00Added an answer on May 17, 2026 at 11:22 pm

    There is no compilation for a replace expression. So about the only thing you can do is exec or eval it with the e flag:

    if($test =~ s/$regex/eval qq["$hash->{subs}"]/e ) { #...
    

    worked for me after changing \\1 to \$1 in the replacement strings.

    s/$regex/$hash->{subs}/
    

    only replaces the matched part with the literal value stored in $hash->{subs} as the complete substitution. In order to get the substitution working, you have to force Perl to evaluate the string as a string, so that means you even have to add the dquotes back in in order to get the interpolating behavior you are looking for (because they are not part of the string.)

    But that’s kind of clumsy, so I changed the replace expressions into subs:

    my @Special_Regex 
        = ( 
            { regex => qr{\s*element\s+/my_elem_removed\s*/main/\d+$}
            , subs  => sub { '#Line removed' }
            }
        ,   { regex => qr{\s*element\s+/my_elem_changed/releases/\.\.\.\s*(.*$)}
            , subs  => sub { 
                return "element  -directory  /my_elem/releases/... $1\n"
                     . "element  /my_elem/releases/.../*.[ch]  $1"
                     ; 
              }
            }
    
        );
    

    I got rid of a bunch of stuff that you don’t have to escape in a substitution expression. Since what you want to do is interpolate the value of $1 into the replacement string, the subroutine does simply that. And because $1 will be visible until something else is matched, it will be the right value when we run this code.

    So now the replacement looks like:

    s/$regex/$hash->{subs}->()/e
    

    Of course making it pass $1 makes it a little more bulletproof, because you’re not depending on the global $1:

    s/$regex/$hash->{subs}->( $1 )/e
    

    Of course, you would change the sub like so:

    subs => sub {
        my $c1 = shift;
        return "element  -directory  /my_elem/releases/... $c1\n"
             . "element  /my_elem/releases/.../*.[ch]  $c1"
             ; 
    }
    

    Just one last note: "\.\.\." didn’t do what you think it did. You just ended up with '...' in the regex, which matches any three characters.

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

Sidebar

Related Questions

Can anyone explain what this mod_rewrite rule is doing? I'm trying to comment the
Can anyone explain the difference between the types mentioned above and some sample usage
Can anyone explain what advantages there are to using a tool like MSBuild (or
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
Can anyone explain in simple words what First and Second Level caching in Hibernate/NHibernate
Can anyone explain the difference between Server.MapPath(.) , Server.MapPath(~) , Server.MapPath(@\) and Server.MapPath(/) ?
Can anyone explain the meaning of and purposes of the values for the SecurityAction
Can anyone explain to me what this means? Run-Time Check Failure #0 - The
Can anyone explain the differences between Protocols and Categories in Objective-C? When do you
Can anyone explain exactly how the Strategy Pattern relates to Inversion of Control?

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.