Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Problem with a text-parsing regex

by tybalt89 (Monsignor)
on May 08, 2022 at 18:12 UTC ( [id://11143673]=note: print w/replies, xml ) Need Help??


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

    The instructions were that "--" was to be treated like a space, so presumably should not be part of the punctuation runs - I think it should print {|o'clock|}, for example, not {|o'clock|--}.

    I do prefer your version of the word parsing to mine, but I suspect (?: '-? | -'? ) is what's intended. (There aren't any examples of "word-'word" in the test cases though - I could probably come up with one in Dutch, but I imagine they're pretty rare in English.)

      I grepped my collection of text files (all English-language downloads from gutenberg.org) for -' and only found forty-'leven and fellow-'prentice. I've updated tybalt89's solution with your improvement.

      The contents of $3 will contain a final space if one is present, so {|o'clock|--} is consistent with the instructions.

Re^2: Problem with a text-parsing regex
by ibm1620 (Hermit) on May 08, 2022 at 23:44 UTC
    Yes! Beautiful. Makes perfect sense. Thank you.

    Your final print is a real head-scratcher, but I like what it does and I'll noodle on it some more....

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-16 06:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found