Perl regular expression - eliminate - regex

Am a newbie here. I use glimpse in my Perl script to get the path of files.
For example
/home/user/Proj/A/Apps/App.pm
/home/user/Proj/B/Apps.pm
I need to fetch the part after Proj i.e; the output should be
A/Apps/App.pm
B/Apps.pm

If you want to use regex/replace you could do something like:
$str =~ s!.*/Proj/!!;

You have various options here. When it's always at /home/user/Proj/, I prefer the second way. If not, you can use the first way as well. The best way is a substr (when its a static length):
use 5.014;
use strict;
use warnings;
my $s_a = "/home/user/Proj/A/Apps/App.pm";
my $s_b = "/home/user/Proj/B/Apps.pm";
say $s_a =~ s{.*Proj/}{}r;
say $s_b =~ s{.*Proj/}{}r;
say $s_a =~ s{/home/user/Proj/}{}r;
say $s_b =~ s{/home/user/Proj/}{}r;
say substr $s_a, 16;
say substr $s_b, 16;
output:
A/Apps/App.pm
B/Apps.pm
A/Apps/App.pm
B/Apps.pm
A/Apps/App.pm
B/Apps.pm

If you want to modifiy an existing variable to remove the first part of the path then it's simple: just use the substitution operator s/// to remove the first part of the string up to /Proj/. I've used alternative delimiters s||| here to avoid having to escape the slashes in the pattern.
use strict;
use warnings;
my #paths = qw{
/home/user/Proj/A/Apps/App.pm
/home/user/Proj/B/Apps.pm
};
for my $path (#paths) {
$path =~ s|.*/Proj/||;
print $path, "\n";
}
output
A/Apps/App.pm
B/Apps.pm
But if you want to leave your path variable as it is and copy the tail portion to another variable, then I think it's best to use a regular expression to capture the wanted part, like this
for my $path (#paths) {
my ($tail) = $path =~ m|/Proj/(.+)|;
print $tail, "\n";
}
The output is identical.

Related

How do I do a Perl regex that matches everything after a backslash and before a certain file extension?

"..\directory\[filename].xml"
How would I go about a writing a regex that matches the above string with the quotes included? How do I match up to the .xml?
So far I've got the following. Not sure how to proceed after this, I'm pretty new to Perl.
$content =~ m/"..\\directory\\/
Thanks!
Method 1 (using regexes)
#!/usr/bin/perl
use strict;
use warnings;
my $path = "..\\directory\\somefile.xml";
if(my ($before) = $path =~ m{^(.*)\.xml$}) {
print "[$before] match!\n";
} else {
print "no match!\n";
};
Output:
[..\directory\somefile] match!
Method 2 (using File::Spec::Win32)
This method makes use of the built-in module File::Spec that comes with every Perl distribution.
This can be better than the first method because it's a specialized module for handling paths.
The built-in module File::Basename is also worth looking at.
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec::Win32;
my $path = "..\\directory\\somefile.xml";
my ($volume,$directories,$file) = File::Spec::Win32->splitpath($path);
print "vol=$volume\n";
print "dirs=$directories\n";
print "file=$file\n";
Output:
vol=
dirs=..\directory\
file=somefile.xml
The normal regex for matching the base name component is this.
m{([^\\/]+)$}
In your case you would modify that a little like this.
m{([^\\/]+\.xml)"$}

Perl special variables for regex matches

I'd like to use one of perl's special variable to make this snippet a bit less large and ugly:
my $mysqlpass = "mysqlpass=verysecret";
$mysqlpass = first { /mysqlpass=/ } #vars;
$mysqlpass =~ s/mysqlpass=//;
I have looked this info up and tried several special variables ($',$1,$`, etc) to no avail
A s/// will return true if it replaces something.
Therefore, it is possible to simply combine those two statements instead of having a redundant m//:
use strict;
use warnings;
use List::Util qw(first);
chomp(my #vars = <DATA>);
my $mysqlpass = first { s/mysqlpass=// } #vars;
print "$mysqlpass\n";
__DATA__
mysqluser=notsosecret
mysqlpass=verysecret
mysqldb=notsecret
Outputs:
verysecret
One Caveat
Because $_ is an alias to the original data structure, the substitution will effect the #vars value as well.
Alternative using split
To avoid that, I would inquire if the #vars contains nothing but key value pairs separated by equal signs. If that's the case, then I would suggest simply translating that array into a hash instead.
This would enable much easier pulling of all keys:
use strict;
use warnings;
chomp(my #vars = <DATA>);
my %vars = map {split '=', $_, 2} #vars;
print "$vars{mysqlpass}\n";
__DATA__
mysqluser=notsosecret
mysqlpass=verysecret
mysqldb=notsecret
Outputs:
verysecret
Yeah, regular expression it, if you really want to visit the path of obfuscation.
See following code:
my $string = "mysqlpass=verysecret";
if ($string =~ /^(\w+)\=(\w+)$/) {
print $1; # This stores 'mysqlpass'
print $2; # This stores 'verysecret'
}
My recommendation against this though, is that you want your code to be readable.
The one you're looking for is $_.

Perl regex to find keywords and not variables

I'm trying to create a regex as following :
print $time . "\n"; --> match only print because time is a variable ($ before)
$epoc = time(); --> match only time
My regex for the moment is /(?-xism:\b(print|time)\b)/g but it match time in $time in the first example.
Check here.
I tried things like [^\$] but then it doesn't match print anymore.
(I will have more keyword like print|time|...|...)
Thanks
Parsing perl code is a common and useful teaching tool since the student must understand both the parsing techniques and the code that they're trying to parse.
However, to do this properly, the best advice is to use PPI
The following script parses itself and outputs all of the barewords. If you wanted to, you could compare the list of barewords to the ones that you're trying to match. Note, this will avoid things within strings, comments, etc.
use strict;
use warnings;
use PPI;
#my $src = do {local $/; <DATA>}; # Could analyze the smaller code in __DATA__ instead
my $src = do {
local #ARGV = $0;
local $/;
<>;
};
# Load a document
my $doc = PPI::Document->new( \$src );
# Find all the barewords within the doc
my $barewords = $doc->find( 'PPI::Token::Word' );
for (#$barewords) {
print $_->content, "\n";
}
__DATA__
use strict;
use warnings;
my $time = time;
print $time . "\n";
Outputs:
use
strict
use
warnings
use
PPI
my
do
local
local
my
PPI::Document
new
my
find
for
print
content
__DATA__
What you need is a negative lookbehind (?<!\$), it's zero-width so it doesn't "consume" characters.
(?<!\$)a means match a if not preceded with a literal $. Note that we escaped $ since it means end of string (or line depending on the m modifier).
Your regex will look like (?-xism:\b(?<!\$)(print|time)\b).
I'm wondering why you are turning off the xism modifiers. They are off by default.So just use /\b(?<!\$)(?:print|time)\b/g as pattern.
Online demo
SO regex reference

Perl's grep gives different results when used with or without qr

I'm trying to use grep with a variable regex. I assumed that the following program would print nothing, since the regex /food/ doesn't match any of the items in my array.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
my #arry = qw/foo bar baz/;
my $regex = "food";
say join ",", grep { qr/$regex/ } #arry;
But the output says otherwise:
foo,bar,baz
When I take out the qr, I get the results I expected (i.e. nothing matches). What is qr doing to cause this?
qr// is a value, and doesn't match against $_. A regex object is always true in boolean context.
You want to apply the regex, e.g like
grep $_ =~ qr/$regex/, #array;
but that is silly. Use the normal m// match operator or variations of it:
grep /$regex/, #array;
This should then produce empty output.
The qr// quote operator makes composing regexes easier, as it has the same parsing rules as ordinary regexes in m// or s///. The value of qr// literals is a regexp object, which can be assigned to a variable, and can then be interpolated. This allows code like
my $foobarbaz = qr/\s*(?:foo|bar|baz)\s*/; # not regexp parsing rules at work for \s
local $_ = "bar foo baz";
say m/${foobarbaz}{2,}/ ? 1 : 0; # use this to compose a regex
As amon said in his answer, the qr// construct creates a regex object which is never false, so the use of grep { qr/anything/ } #arry selects everything in the array.
This is how you might use a qr regex:
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
my #arry = qw/foo bar baz dog food/;
my $regex = "food";
my $qr = qr/$regex/;
say "[", join ",", grep({ qr/$regex/ } #arry), "]"; # Original
say "[", join ",", grep({ $_ =~ $qr } #arry), "]"; # Modified
say $qr; # Stringized
The output is as you'd expect:
[foo,bar,baz,dog,food,]
[food,]
(?^:food)
The parentheses are necessary to limit the scope of the grep (otherwise, the "]" is passed as an extra argument to grep, not say.
Instead of using qr// in the grep block, use it in the definition of your $regex variable. Since qr// is a value (in particular, a compiled regex), it doesn't actually attempt to match anything. Since you've put the qr// in a boolean context, it will always be true.
It might be easier to just use the normal m// operator like this: grep /$regex/, #arry;
If for some reason you really want to have a precompiled regex before using grep against the array, try this instead.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
my #arry = qw/foo bar baz/;
my $regex = qr/food/;
say join ",", grep { /$regex/ } #arry;
Refer to the documentation for some examples on how the qr// operator works.

How to pass a replacing regex as a command line argument to a perl script

I am trying to write a simple perl script to apply a given regex to a filename among other things, and I am having trouble passing a regex into the script as an argument.
What I would like to be able to do is somthing like this:
> myscript 's/hi/bye/i' hi.h
bye.h
>
I have produced this code
#!/utils/bin/perl -w
use strict;
use warnings;
my $n_args = $#ARGV + 1;
my $regex = $ARGV[0];
for(my $i=1; $i<$n_args; $i++) {
my $file = $ARGV[$i];
$file =~ $regex;
print "OUTPUT: $file\n";
}
I cannot use qr because apparently it cannot be used on replacing regexes (although my source for this is a forum post so I'm happy to be proved wrong).
I would rather avoid passing the two parts in as seperate strings and manually doing the regex in the perl script.
Is it possible to pass the regex as an argument like this, and if so what is the best way to do it?
There's more than one way to do it, I think.
The Evial Way:
As you basically send in a regex expression, it can be evaluated to get the result. Like this:
my #args = ('s/hi/bye/', 'hi.h');
my ($regex, #filenames) = #args;
for my $file (#filenames) {
eval("\$file =~ $regex");
print "OUTPUT: $file\n";
}
Of course, following this way will open you to some very nasty surprises. For example, consider passing this set of arguments:
...
my #args = ('s/hi/bye/; print qq{MINE IS AN EVIL LAUGH!\n}', 'hi.h');
...
Yes, it will laugh at you most evailly.
The Safe Way:
my ($regex_expr, #filenames) = #args;
my ($substr, $replace) = $regex_expr =~ m#^s/((?:[^/]|\\/)+)/((?:[^/]|\\/)+)/#;
for my $file (#filenames) {
$file =~ s/$substr/$replace/;
print "OUTPUT: $file\n";
}
As you can see, we parse the expression given to us into two parts, then use these parts to build a full operator. Obviously, this approach is less flexible, but, of course, it's much more safe.
The Easiest Way:
my ($search, $replace, #filenames) = #args;
for my $file (#filenames) {
$file =~ s/$search/$replace/;
print "OUTPUT: $file\n";
}
Yes, that's right - no regex parsing at all! What happens here is we decided to take two arguments - 'search pattern' and 'replacement string' - instead of a single one. Will it make our script less flexible than the previous one? No, as we still had to parse the regex expression more-or-less regularly. But now user clearly understand all the data that is given to a command, which is usually quite an improvement. )
#args in both examples corresponds to #ARGV array.
The s/a/b/i is an operator, not simply a regular expression, so you need to use eval if you want it to be interpreted properly.
#!/usr/bin/env perl
use warnings;
use strict;
my $regex = shift;
my $sub = eval "sub { \$_[0] =~ $regex; }";
foreach my $file (#ARGV) {
&$sub($file);
print "OUTPUT: $file\n";
}
The trick here is that I'm substituting this "bit of code" into a string to produce Perl code that defines an anonymous subroutine $_[0] =~ s/a/b/i; (or whatever code you pass it), then using eval to compile that code and give me a code reference I can call from within the loop.
$ test.pl 's/foo/bar/' foo nicefood
OUTPUT: bar
OUTPUT: nicebard
$ test.pl 'tr/o/e/' foo nicefood
OUTPUT: fee
OUTPUT: nicefeed
This is more efficient than putting an eval "\$file =~ $regex;" inside the loop as then it'll get compiled and eval-ed at every iteration rather than just once up-front.
A word of warning about eval - as raina77ow's answer explains, you should avoid eval unless you're 100% sure you are always getting your input from a trusted source...
s/a/b/i is not a regex. It is a regex plus substitution. Unless you use the string eval, make this work might be pretty tough (consider s{a}<b>e and so on).
The trouble is that you are trying to pass a perl operator when all you really need to pass is the arguments:
myscript hi bye hi.h
In the script:
my ($find, $replace, #files) = #ARGV;
...
$file =~ s/$find/$replace/i;
Your code is a bit clunky. This is all you need:
use strict;
use warnings;
my ($find, $replace, #files) = #ARGV;
for my $file (#files) {
$file =~ s/$find/$replace/i;
print "$file\n";
}
Note that this way allows you to use meta characters in the regex, such as \w{2}foo?. This can be both a good thing and a bad thing. To make all characters intepreted literally (disable meta characters), you can use \Q ... \E like so:
... s/\Q$find\E/$replace/i;