in reply to File regex question

Try this

perl -pe"s[ (?<=\\|/) (.*?) (?=\\|/) ][ (my$x=$1) =~ s{ }{_}g; $x]gex" + file

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

Replies are listed 'Best First'.
Re: Re: File regex question
by Anonymous Monk on Sep 16, 2003 at 08:54 UTC
    Thanks!
    I would appreciate if you could tell me how to implement the same in a program? I would start with open FH, "<file";
      I would start with open FH, "<file";

      No, you should not. You should always test the return value of open. Furthermore, I wouldn't use second class variables if I can avoid it. Finally, you may prefer 3-arg open over magic 2-arg open:

      open my $fh, "<", "file" or die "Failed to open 'file': $!";

      Abigail

      Something like this (untested) code may be what your looking for.

      #! perl -w use strict; open IN, '<', 'file' or die $!; open OUT, '>', 'temp' or die $!; while( <IN> ) { s[ (?<=\\|/) (.*?) (?=\\|/) ][ (my$x=$1) =~ s{ }{_}g; $x ]gex; print OUT; } close IN; close OUT; rename 'file', 'file.bak'; rename 'temp', 'file';

      You could also look at using $INPLACE_EDIT ($^I) to have perl do the backup and rename for you.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.