Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Regex with variables?

by Quadzilla (Initiate)
on Aug 22, 2003 at 14:52 UTC ( #285800=perlquestion: print w/replies, xml ) Need Help??

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

Can I do something like this (and have it work ;):

$foo = s/ $scalar /pattern/;

In other words, I want to have the search pattern be a variable. I wrote a command line utility for use on a Win32 platform and computernames all come back in the form "domain-computer," which is useless information. To compress the column, I want to remove the "domain-". Easy enough except I would like my code to adapt to other Windows AD domains, so when it is used by others, it will do the same for them.

Thanks and all respect to the Monks,


Replies are listed 'Best First'.
Re: Regex with variables?
by gjb (Vicar) on Aug 22, 2003 at 14:55 UTC

    Your solution will work if you replace the = by =~.

    Hope this helps, -gjb-

      Thanks gjb, I feel like an idiot... I've come unbound!
Re: Regex with variables?
by tcf22 (Priest) on Aug 22, 2003 at 14:59 UTC
    Use qr//. It is used to store a regex in a variable. Think you want something like this.
    my $re = qr/.+\-(.+)/; $foo =~ s/$re/$1/;
    but if all are in domain-computer form, you could just use
    $foo =~ s/.+\-(.+)/$1/;
    Update: but on second thought the first way could run in a loop and you could just define the regex before the loop using qr//. That would probably be more efficient.

    use strict; my $re = qr/.+\-(.+)/; while(<DATA>){ s/$re/$1/; print; } __DATA__ domain-comp1 domain-comp2 domain-comp3
    comp1 comp2 comp3
      Thanks tcf22! Thanks for taking the time to answer my question! That's exactly what I was trying to achieve.

      “Pie Iesu Domine. Dona eis Requiem.” Whack!
Re: Regex with variables?
by tachyon (Chancellor) on Aug 22, 2003 at 15:46 UTC

    The only issue with using a var as the pattern (other than your missing =~ or course) is that it MAY contain regex metachars. quotemeta and its RE friends \Q and \E are your friends. Do:

    $foo =~ s/\Q$find\$E/$replace/g;




Re: Regex with variables?
by CombatSquirrel (Hermit) on Aug 22, 2003 at 15:53 UTC
    Adiing to what the others said, if $scalar does not contain a RegEx, but a string that you want to match, you might be interested in the quotemeta(...) function, which avoids, for example, $scalar = '.*'; to match the whole string and transforms it to \.\*. Just do something like
    # first possibility $scalar = quotemeta('.*'); $foo =~ s/$scalar/string/; # second possibility chomp($scalar = <>); $scalar = quotemeta($scalar); $foo =~ s/$scalar/string/;
    Please note also that a subtitution substitues a pattern by a string, and not by a pattern. (This is probably what you meant, but some people try things like s!<a>\d{3}</a>!\d{3}! and wonder why it doesn't work)
    Cheers, CombatSquirrel.
Re: Regex with variables?
by Not_a_Number (Prior) on Aug 22, 2003 at 18:51 UTC

    ...and here's a non-regex solution:

    while ( <DATA> ) { my $comp = (split /-/)[-1]; print $comp; } __DATA__ domainA-comp1 domainB-comp2 domainC-comp3


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://285800]
Approved by gjb
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2020-05-25 06:17 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (143 votes). Check out past polls.