Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Help with a regular expression for file name parsing

by Anonymous Monk
on Dec 07, 2011 at 07:19 UTC ( #942171=note: print w/ replies, xml ) Need Help??


in reply to Help with a regular expression for file name parsing

Surely such a format has a parser already , but anyway, I'm sure this will work, untested

my $pat = qr~ \@include \s+ ( (?: '[^']*' ) | (?: "[^"]*" ) | (?: (?:\\.) | [^\\s] )+ ) ~x;

[^" makes a good search term to find regex for similar formats, like ?node_id=3989;BIT=%5B%5E%22 -> Re^3: More robust link finding than HTML::LinkExtor/HTML::Parser?, Re: skip over an escaped single quote


Comment on Re: Help with a regular expression for file name parsing
Select or Download Code
Re^2: Help with a regular expression for file name parsing
by Anonymous Monk on Dec 07, 2011 at 12:59 UTC

    and tested, though i had forgot to escape an \ in [^\\s]

    #!/usr/bin/perl -- #~ 2011-12-07-04:10:56PDT by Anonymous Monk #~ perltidy -csc -otr -opr -ce -nibc -i=4 use strict; use warnings; use autodie; # dies if open/close... fail Main( @ARGV ); exit( 0 ); sub Main { if ( @_ == 2 ) { NotDemoMeaningfulName(@_); } else { Demo(); print '#' x 33 ,"\n", Usage(); } } ## end sub Main sub NotDemoMeaningfulName { my ( $inputFile, $outputFile ) = @_; open my ($inFh), '<', $inputFile; open my ($outFh), '>', $outputFile; while( defined( my $data = <$inFh>) ){ print $outFh "$_\n" for $data =~ m~ \@include \s+ ( (?: '[^']*' ) | (?: "[^"]*" ) | (?: (?:\\.) | [^\\\s] )+ ) ~xg; #~ for $data =~ m[\@include\s('[^']+'|"[^"]+"|.+?(?<!\\))\ +s]g; # /\@include\s+('[^']+'|"[^"]+"|.+?(?<!\\))\s+ +/g } close $inFh; close $outFh; } ## end sub NotDemoMeaningfulName sub Usage { <<"__USAGE__"; $0 $0 dataFile newDataFile __USAGE__ } ## end sub Usage sub Demo { my ( $Input, $WantedOutput ) = DemoData(); NotDemoMeaningfulName( \$Input, \my $Output ); require Test::More; Test::More::is( $Output, $WantedOutput, ' NotDemoMeaningfulName Works Aas Designed' ); Test::More::done_testing(); print "\n$Output\n"; } ## end sub Demo sub DemoData { #~ http://perlmonks... my $One = <<'__One__'; @include test #some "random stuff" @include "some file" did you parse that? #more 'random' stuff @include 'another file' you sure? #and more random stuff @include yet\ another\ file positive? __One__ #~ http://perlmonks... my $Two = <<'__Two__'; test "some file" 'another file' yet\ another\ file __Two__ return $One, $Two; } ## end sub DemoData __END__ $ perl pm.re.942167.pl ok 1 - NotDemoMeaningfulName Works Aas Designed 1..1 test "some file" 'another file' yet\ another\ file ################################# pm.re.942167.pl pm.re.942167.pl dataFile newDataFile

      and tested

      Not tested enough, I'm afraid. Your code doesn't handle properly even such trivial cases as

      @include file

        Not tested enough, I'm afraid. Your code doesn't handle properly even such trivial cases as @include file

        Well that is nonsense, its the first thing tested (@include test)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://942171]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2015-07-03 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (53 votes), past polls