Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I wanted to break input on spaces in an RE, but according to additional rules:
1) Spaces between single or double quotes are not broken in.
2) A single BS ignores any special-ness of the next character.
3) Any quoted string is considered terminated by an end-of-string.

I think (hope) that's it. I've evolved an example I found on this board, but don't remember the original URL. It had some failing, where it didn't satisfy the above additional rules. That example is:

qr{((?>\\[^\\])|"(?:\\\\"|[^\\"]*)"|(?:'[^\\']*'|\S+))+}

My test inputs included "embedded case/line# answer,text" and which assumes testprog can remove '#comments' are:

# ln#, ans, test text 1 3,This is simple. 2 3,This is "so very simple". 3 4,This "is so" very simple. 4 2,This 'isn\'t nice.' 5 2,This "isn\"t nice." 6 3,This 'isn\\'t nice.' 7 3,This "isn\\"t nice." 8 2,This "isnt wrong." 9 2,This 'isnt wrong.' 10 3,This 'isn\\'t wrong. 11 3,This "isn\\"t wrong. 12 3,This isn\'t horrible. 13 3,This isn\"t terrible. 14 3,This \"isnt unnice.\" 15 3,This \'isnt unnice.\' 16 2,This 'is not unnice.' 17 2,This "is not unnice." 18 3,a "bb cc" d

So far, my sample test-runs show lines/cases 4+5 to be in error.

That is the main problem, the rest is specific to my testing program, which I don't care about, but can be used to run the sample RE against the test cases (which are included in the source of the testing program). The test prog's output follows (Note -- like the testprog, don't care about the format, it was just to show me pass/fails"):

ResByLn:{ln=>1, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>2, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>3, wanted=>4, got=>[4]},[" p "] ResByLn:{ln=>4, wanted=>2, got=>[3]},["FAIL:<This "isn\"t nice.">"] ResByLn:{ln=>5, wanted=>2, got=>[3]},["FAIL:<This 'isn\\'t nice.'>"] ResByLn:{ln=>6, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>7, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>8, wanted=>2, got=>[2]},[" p "] ResByLn:{ln=>9, wanted=>2, got=>[2]},[" p "] ResByLn:{ln=>10, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>11, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>12, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>13, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>14, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>15, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>16, wanted=>2, got=>[2]},[" p "] ResByLn:{ln=>17, wanted=>2, got=>[2]},[" p "] ResByLn:{ln=>18, wanted=>3, got=>[3]},[" p "]
The test prog originally was written to handle multiple RE's, but I threw out all the ones that were less successful than the 1 remaining -- but that's why some things are bracketed like \got\, "array values" in the test-prog output.

For FAIL cases, I had it print the failing text, just to be sure I was failing on the line I thought I was.

The test prog takes an optional '-f' that filters output to only display the FAILing cases.

I repeat -- my main problem is finding an RE that I can use to break the test-cases into the correct number of capture-args (as broken by unquoted spaces). Am including my test prog, below for those that want to use it or see what I did. But the help I need is in fixing the 'RE' so all the test cases work. Thanks for any help!

#!/usr/bin/perl use P; # vim=:SetNumberAndWidth if ( -t 0) { # unlikely to provide reliable test case open(STDIN, "<&=", "main::DATA") || die "opening internal tests: $!" +; } my @lines = grep { defined $_ and ! /^\s*#/ } (<main::DATA>); my @regex = ( qr{((?>\\[^\\])|"(?:\\\\"|[^\\"]*)"|(?:'[^\\']*'|\S+))+} + ); my $ln=0; my $norm=0; my @ResByLn; sub lnout($$$) { my ($ans,$outp, $lnp) = @_; bless {wanted=>$ans, got=>$outp, ln=>$lnp}, q(ResByLn:); } sub txt($) { local $_=shift; my (undef, undef,$txt)=m{^\s*(\d+)\s+(\d+),(.*)}; $txt; } my $only_fails = @ARGV && $ARGV[0] eq '-f' ? 1 : 0; for (@lines) { ++$ln; my ($lnnum, $ans,$_)=m{^\s*(\d+)\s+(\d+),(.*)}; my @got; for (my $r=0; $r<@regex; ++$r) { my $reg = $regex[$r]; my @out = grep {$_ } m{$reg}g; my $cnt = 0+@out; push @got, $cnt; } $lnnum and push @outs, lnout($ans, \@got, $lnnum); } for my $o (@outs) { my $ans = $o->{wanted}; my @out = @{$o->{got}}; my $ln = $o->{ln}; my @rts; #results for (my $i=0;$i<@out;++$i) { $rts[$i] = do { if ($ans==$out[$i]) { " p " } else { "FAIL:" . do { my $txt = txt($lines[$ln]); chomp $txt; "<$txt>"; }; } }; } my @output_args=("%s,%s",$o,\@rts); # * Shows line-number, result-wanted, result-got + test-status # * if test-status is FAIL, shows corresponding test-txt between <> +(carats) # unless ($only_fails) { P @output_args; } else { my @fails = grep /FAIL/, P @output_args; foreach (@fails) { P "%s", $_; } } } # vim: ts=2 sw=2 ai number __DATA__ # ln#, ans, test text 1 3,This is simple. 2 3,This is "so very simple". 3 4,This "is so" very simple. 4 2,This 'isn\'t nice.' 5 2,This "isn\"t nice." 6 3,This 'isn\\'t nice.' 7 3,This "isn\\"t nice." 8 2,This "isnt wrong." 9 2,This 'isnt wrong.' 10 3,This 'isn\\'t wrong. 11 3,This "isn\\"t wrong. 12 3,This isn\'t horrible. 13 3,This isn\"t terrible. 14 3,This \"isnt unnice.\" 15 3,This \'isnt unnice.\' 16 2,This 'is not unnice.' 17 2,This "is not unnice." 18 3,a "bb cc" d __END__
P.s. I could probably do this easily in a parser -- But I'm trying to fit it into an RE, as I think it should be possible, and/or I just maybe a self-masochist. :-0 P.P.s originally had 4,5,6,7 as wrong, but realize 6+7 were right, so corrected things (I hope).

In reply to solution wanted for break-on-spaces (w/specifics) by perl-diddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-23 10:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found