regex help with 'log*' in it

E

ebm

Does anybody know who to get this to work if the $sufix has a * in it.
Would I have to have the script do something like this?
if ($sufix =~ /\*/) {
if( /^\Q$prefix/ && /$sufix*$/ ) {
print("$_ \n");
}
}

Any ideas?


# look for files
my $sufix = '.log*'; #added for testing, normally read in from a
config file
my $prefix = 'blah'; #added for testing, normally read in from a
config file

my @files;
opendir(LOCALLOG,$path) || die("Unable to open $path, Verify the path
exists:$!"); #open Local Path log directory
@files = grep {-f "$path/$_"} readdir(LOCALLOG); # get array of
files
close(LOCALLOG);

foreach(@files){
if( /^\Q$prefix/ && /$sufix$/ ) {
print("$_ \n");
}
}
 
J

John W. Krahn

ebm said:
Does anybody know who to get this to work if the $sufix has a * in it.
Would I have to have the script do something like this?
if ($sufix =~ /\*/) {
if( /^\Q$prefix/ && /$sufix*$/ ) {
print("$_ \n");
}
}

No, just use \Q for both the prefix and the sufix:

if ( /^\Q$prefix\E/ && /\Q$sufix*\E$/ ) {
print "$_\n";
}



John
 
E

ebm

I tried adding the /\Q$sufix*\E$/
and it does not pickup any of the file names.
If I set the $sufix = '.log' I still get nothing. When I had it like
this /^\Q$prefix\E/ && /$sufix$/ I would get my the exact make but not
the *.

blah20060312.log
 
E

ebm

I tried it like this and I get results with $sufix = '.log' but not
'.log*'
/^\Q$prefix\E/ && /\Q$sufix\E$/
 
A

Anno Siegel

ebm said:
I tried it like this and I get results with $sufix = '.log' but not
'.log*'
/^\Q$prefix\E/ && /\Q$sufix\E$/

What are the file names you expect it to match that it doesn't? Do you
actually have files ending in a literal asterisk?

Anno
 
I

it_says_BALLS_on_your forehead

Anno said:
What are the file names you expect it to match that it doesn't? Do you
actually have files ending in a literal asterisk?


to Anno's point, check out man ls, the -F option:

-F Marks directories with a trailing slash (/), doors
with a trailing greater-than sign (>), executable
files with a trailing asterisk (*), FIFOs with a
trailing vertical bar (|), symbolic links with a
trailing at-sign (@), and AF_UNIX address family sock-
ets with a trailing equals sign (=).


....check out your aliases...
 
E

ebm

I don't expect the file name to end in * but I'm expected for the code
to hand a requset to search for .log* (greedy wildcard)

exYYYYMMDD.log
exYYYYMMDD.log-other-data
The * should try and catch this kind of stuff.

the sufix name will be collected from a config file and I need to
expect somebody using * to look for everything after .log
 
J

John W. Krahn

ebm said:
I don't expect the file name to end in * but I'm expected for the code
to hand a requset to search for .log* (greedy wildcard)

exYYYYMMDD.log
exYYYYMMDD.log-other-data
The * should try and catch this kind of stuff.

the sufix name will be collected from a config file and I need to
expect somebody using * to look for everything after .log


$sufix = s/\*/[^.]*/;

if ( /^\Q$prefix\E/ && /$sufix$/ ) {
print "$_\n";
}


John
 
K

Kraven

ebm said:
I don't expect the file name to end in * but I'm expected for the code
to hand a requset to search for .log* (greedy wildcard)

exYYYYMMDD.log
exYYYYMMDD.log-other-data
The * should try and catch this kind of stuff.

the sufix name will be collected from a config file and I need to
expect somebody using * to look for everything after .log

and now for something completely different...

foreach $file (<$path/log*>) {
print $file,"\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top