Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Filename change with regular expression

by jfroebe (Parson)
on Aug 09, 2013 at 20:12 UTC ( #1048840=note: print w/replies, xml ) Need Help??


in reply to Re^2: Filename change with regular expression
in thread Filename change with regular expression

but I am trying to do all in one regular expression.

Why? Other than decreasing readability and maintainability of the code, I see no purpose of having it as a single regular expression.

Jason L. Froebe

Blog, Tech Blog

  • Comment on Re^3: Filename change with regular expression

Replies are listed 'Best First'.
Re^4: Filename change with regular expression
by Laurent_R (Abbot) on Aug 09, 2013 at 21:51 UTC

    Other than decreasing readability and maintainability of the code, I see no purpose of having it as a single regular expression.

    I definitely agree. It is often much better to have several regexes is a row, for example to just get rid of all exceptions and special cases, before the regex doing the actual work, rather than trying to pack everything into a single complicated regex.

    For example, suppose that you don't want to apply your substitution on file lines that start with XYZ, contain ZYX or XZY or end with YZZ. Trying to put all this in your single s/// substitution is quickly becoming a nighmare, while something like this is very clear:

    while (<$IN>) { next if /^XYZ/ or /ZYX/ or /XZY/ or /YZZ$/; # removes all special + cases where substitution should not occur s/foo/bar/g; # do something with $_ }

    In the case of the OP, this might be something like this:

    foreach $file (@filelist) { next unless file =~ /_00001\.\w+$/; # now only start for looking for a random number to replace _0000 +1 # ... }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1048840]
help
Chatterbox?
[stevieb]: ffs... an hour I've been trying to get a Pi to communicate I2C to an Arduino. Had to slow down the communication channel. Sigh

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2017-06-22 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (531 votes). Check out past polls.