Hi,

I try to use a specific word in my file to put it as a folder name.
This word vary in my file so, I think about use regular expressions to define it.

my $file=$ARGV[0];
my $word=~/DOT\d+/;
my $line="";

open(file, $file);

while( $line = <file>){
    if ( $line =~ /^Number\s=\s$word/){
        unless(-d $word){
            mkdir $word,0755;
            chdir $word or die "can't change directory";
        }
    }
}   

close GSE;

Unfortunately, that doesn't work.

Can Perl understand regular expressions in a variable and understand a regular expression in a regular expression?

Thanks for help,
Perlie

my $word=~/DOT\d+/; attempts to match whatever is in $word and there is nothing in $word at this point in the script.

You can store a regex pattern in a variable using the Regexp-like quote function as follows: my $word = qr(/DOT\d+/);

Sorry, the above example of using qr to store a pattern is not quite correct. You could store your pattern as follows:
my $word = qr(DOT\d+);

For example:

#!/usr/bin/perl
use strict; 
use warnings;

my $string = 'Number = DOT123';
my $word = qr(DOT\d+);

if ($string =~ m/^Number\s=\s$word/){
    print "$string matches the $word pattern\n"
}
else{
    print "$string does not match the $word pattern\n"
}

I will try this as soon as possible and give you a feedback. Thank you d5e5.

If you also want to capture (or extract) the portion of the string that matches your pattern, then have a look at Extracting Matches.

So great! I successed in my job thanks to these Extracting Matches. Very simple in fact. Thank you again Mr d5e5!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.