Re: regex needed with match operator
by broquaint (Abbot) on Apr 28, 2003 at 13:37 UTC
|
Here's a fix for your regex
if(m[/\w+/foo]) {
s[.* /(\w+) /foo \z]($1)x;
...
}
Here's a simpler solution which grabs what is matched (as opposed to modifying the contents @FILES as your code does)
print m{ /(\w+) /foo\z }x, "\n"
for @FILES;
See. perlre for more info on the regex used.
HTH
_________ broquaint | [reply] [d/l] [select] |
|
And once you try to store the retrieved bits, you arrive at something like
my @name;
push @name, m{ /(\w+) /foo\z }x
for @FILES;
which, of course, is a map in disguise:
my @name = map m{ /(\w+) /foo\z }x, @FILES;
Makeshifts last the longest. | [reply] [d/l] [select] |
|
Thanks broquaint,
Would you mind explaining exactly what the \z and x are doing in the regex?
Thanks again
| [reply] |
|
Would you mind explaining exactly what the \z and x are doing in the regex?
Sure, to quote the docs
\z Match only at end of string
This is very similar to $ except $ matches the end of a line or before a newline.
x Extend your pattern's legibility by permitting whitespace and comments.
As it says, it allows for more readable regex, which is generally desirable considering the acute succinctness of regular expressions.
HTH
_________ broquaint
| [reply] |
|
Re: regex needed with match operator
by rob_au (Abbot) on Apr 28, 2003 at 13:41 UTC
|
The most portable manner for dealing with file names and directory paths is through the use of File::Spec module - For example:
use File::Spec;
my @paths =
(
'C:/WINDOWS/TEST/Vendor/Bomb_USR/foo',
'C:/WINDOWS/TEST/Vendor/Bomb_MOT/foo',
'C:/WINDOWS/TEST/Vendor/Bay_Accelar/foo',
'C:/WINDOWS/TEST/Vendor/BayStackHub/foo',
'C:/WINDOWS/TEST/Vendor/Ascend_4000/foo',
'C:/WINDOWS/TEST/Vendor/Aironet/foo',
'C:/WINDOWS/TEST/Vendor/ADC_Cuda/foo',
'C:/WINDOWS/TEST/Vendor/3ComTokenRing/foo',
);
foreach my $path ( @paths )
{
my $value = ( File::Spec->splitdir( $path ) )[-2];
print $value, "\n";
}
perl -le 'print+unpack("N",pack("B32","00000000000000000000001001010011"))' | [reply] [d/l] |
Re: regex needed with match operator
by LordWeber (Monk) on Apr 28, 2003 at 13:36 UTC
|
| [reply] |
|
/\/(\w+)\/foo$/
Perhaps better as:
m!/([^/]+)/foo$!
Advantages:
- Uses "choose your own quotes" to avoid falling-toothpick syndrome.
- Some systems allow some funky characters that \w won't match. However, '/' certainly won't be in a filename (unless something really, really weird happens).
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
| [reply] [d/l] [select] |
|
| [reply] |
|
|
|
Re: regex needed with match operator
by LordWeber (Monk) on Apr 28, 2003 at 16:06 UTC
|
| [reply] |
|
Read Aristotle's sig. Every application I've ever worked on started out as a "quick and dirty" script. It's always best, unless you are deleting the script within 12 hours, to use the modules.
- You know it will catch the boundary cases you didn't know even existed
- It's quicker
- The more of your <whatever> is stock code, the less you have to debug.
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement. Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
| [reply] |