Reputation: 77
i want a perl regex to grep a word from the following output:
Process Completed;Result= Volume in drive D has no label.
Volume Serial Number is 328A-C899
Directory of D:\Program
07/14/2013 12:09 PM <DIR> .
07/14/2013 12:09 PM <DIR> ..
06/16/2013 01:07 PM <DIR> IPS
07/14/2013 12:10 PM <DIR> IPS1
07/14/2013 12:12 PM <DIR> IPS2
07/14/2013 12:16 PM <DIR> IPS3
07/14/2013 01:50 PM <DIR> IPS4
07/14/2013 12:17 PM <DIR> IPS5
07/14/2013 12:17 PM <DIR> IPS6
07/14/2013 12:18 PM <DIR> IPS7
07/14/2013 12:18 PM <DIR> IPS8
06/16/2013 01:10 PM <DIR> IPSCommon
07/08/2013 12:32 PM <DIR> _IPS10
07/08/2013 12:32 PM <DIR> _IPS11
07/08/2013 12:32 PM <DIR> _IPS12
07/08/2013 12:32 PM <DIR> _IPS13
07/08/2013 12:32 PM <DIR> _IPS14
07/08/2013 12:57 PM <DIR> _IPS15
07/08/2013 12:32 PM <DIR> _IPS16
07/08/2013 03:38 PM <DIR> _IPS17
07/08/2013 12:32 PM <DIR> _IPS18
07/08/2013 12:32 PM <DIR> _IPS9
0 File(s) 0 bytes
22 Dir(s) 770,968,162,304 bytes free
IPS7
and not starting with _
I have used the following regex IPS\d+\d*$
but this will grep words that starts with _
also
How can I specify ! not _
?
Upvotes: 2
Views: 742
Reputation: 67910
You are trying to parse the output of the Windows dir
command, which I assume is an attempt from you to list files in a directory. You should know that this is not a good way to accomplish that, and I will show you some alternatives. The answers to the regex are already given, so I won't bother with that.
Using a glob
, < ... >
, which is pretty much an emulation of how a shell expands wildcards.
my @ips = grep /^IPS\d+/, # only IPS with number
grep -d, # only directories
<D:/Program/IPS*>; # list IPS file in the target dir
Using opendir
.
opendir my $dh, "D:/Program" or die $!;
my @ips = grep /^IPS\d+/, readdir($dh);
closedir $dh;
Using File::Find
. Do note that this option is recursive (will also list files in subdirectories):
use File::Find; # core module in Perl 5
my @ips;
find(sub { push @ips, $File::Find::name if /^IPS\d+/ }, "D:/Program");
Each method has its own advantages. In your case, the most similar method to use would be the top one, the one using a glob.
Upvotes: 4
Reputation: 11
need to capture only Start with IP.
Please use the below regex.
(^IPS\d+)$
Upvotes: 0
Reputation: 1073
add this before your regex
it will look for word boundary and still not include in result before IPS
(?<=\b)
so your final regex looks like
(?<=\b)IPS\d+\d*$
Upvotes: 0
Reputation: 37146
You can use a negated character class:
[^_]IPS\d+$
Note that the second \d
in your original regex is redundant because \d+
will be greedy.
Upvotes: 1