in reply to Problem with a text-parsing regex
Different way to handle --
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11143647 use warnings; while (my $x = <DATA>) { chomp $x; print "\n$x\n"; my $out = ''; while ( $x =~ m/ ([[:punct:]]*) # $1: leading punct marks ( # $2: a "word" consisting of [[:word:]]+ # word (?: (?: '-? | - ) [[:word:]]+ # and ending in pure word characters )* ) ( (?: --+ | [[:punct:]]* ) \ ? ) # $3: trailing punct marks +ending # or multi-dashs # with space (except at end of # line?) /xxg ) { $out .= sprintf "{%s|%s|%s} ", # make whitespace visible map {(my $y = $_ // '') =~ tr/ /_/; $y} $1, $2, $3; } print "$out\n" =~ s/ $//r =~ s/.{65}\K /\n/gr; } __DATA__ "'Uncouth' about sums it up." The word they will use is 'uncouth'. "It's the old story." It's a will-o'-the-wisp--a two-fer--and Bob's-yer-uncle at four o'cloc +k. It's two o'clock--time for a nap. Remember 45's? What about (this)? [Editor's note: blah blah] and so on... A ... and B I said--"What's the expression?"
Outputs (changed to be able to see all output without scrolling):
"'Uncouth' about sums it up." {"'|Uncouth|'_} {|about|_} {|sums|_} {|it|_} {|up|."} The word they will use is 'uncouth'. {|The|_} {|word|_} {|they|_} {|will|_} {|use|_} {|is|_} {'|uncouth|'.} "It's the old story." {"|It's|_} {|the|_} {|old|_} {|story|."} It's a will-o'-the-wisp--a two-fer--and Bob's-yer-uncle at four o'cloc +k. {|It's|_} {|a|_} {|will-o'-the-wisp|--} {|a|_} {|two-fer|--} {|and|_} {|Bob's-yer-uncle|_} {|at|_} {|four|_} {|o'clock|.} It's two o'clock--time for a nap. {|It's|_} {|two|_} {|o'clock|--} {|time|_} {|for|_} {|a|_} {|nap|.} Remember 45's? {|Remember|_} {|45's|?} What about (this)? {|What|_} {|about|_} {(|this|)?} [Editor's note: blah blah] and so on... {[|Editor's|_} {|note|:_} {|blah|_} {|blah|]_} {|and|_} {|so|_} {|on|. +..} A ... and B {|A|_} {|and|_} {|B|} I said--"What's the expression?" {|I|_} {|said|--} {"|What's|_} {|the|_} {|expression|?"}
Did get every thing right?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Problem with a text-parsing regex
by hv (Prior) on May 09, 2022 at 01:22 UTC | |
by ibm1620 (Chaplain) on May 09, 2022 at 13:16 UTC | |
|
Re^2: Problem with a text-parsing regex
by ibm1620 (Chaplain) on May 08, 2022 at 23:44 UTC | |
by kcott (Archbishop) on May 11, 2022 at 08:23 UTC |