Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Regex with variables?

by tcf22 (Priest)
on Aug 22, 2003 at 14:59 UTC ( [id://285803]=note: print w/replies, xml ) Need Help??


in reply to Regex with variables?

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.

Example:
use strict; my $re = qr/.+\-(.+)/; while(<DATA>){ s/$re/$1/; print; } __DATA__ domain-comp1 domain-comp2 domain-comp3
Output:
comp1 comp2 comp3

Replies are listed 'Best First'.
Re: Re: Regex with variables?
by Quadzilla (Initiate) on Aug 22, 2003 at 15:13 UTC
    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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://285803]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-19 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found