Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Thanks! I thought I'd used that before but perlop having several examples of m/\G.../g and no examples of s/\G...//g and no mention of \G in the s/// section combined to make me think maybe that was the problem (as I was posting -- writing up a problem always brings new solutions to mind, ya know).

I verified that this works on my version of Perl as well.

I'd been meaning to post this for a week and finally had a few minutes while waiting for stuff to compile. This response got me to take some time I didn't have to write up some test cases. I must have had an unseen bug in the code when I was testing before because it works fine now.

Thanks, blakem.

                - tye
# my $len= 79; my $len= 9; # my $esc= '\e'; my $esc= '@'; my $eseq= qr[$esc[^a-zA-Z]*[a-zA-Z]]; my $char= qr[(?:$eseq)*[^$esc\n]]; my $nonsp= qr[(?:$eseq)*[^$esc\s]]; local( $/ )= ''; # Paragraph mode while( <DATA> ) { print "Wrapping:\n$_"; { local( $_ )= $_; s[(?:^|(?<=\s))((?:$char){1,$len}(?:$eseq)*)\s][$1\n]g; # print "Loose code1:\n$_"; s[(?:^|(?<=\s))((?:$nonsp){$len}(?:$eseq)*)(?=[^$esc\s])][$1\n +]g; print "Loose code:\n$_"; } s[(?:\G|^)((?:$char){1,$len}(?:$eseq)*)\s][$1\n]gm; # ^^^^ ^ # print "Tight code1:\n$_"; # vv s[(?:\G|(?<=\s))((?:$nonsp){$len}(?:$eseq)*)(?=[^$esc\s])][$1\n]g; print "Tight code:\n$_"; } __END__ @[0;7mCoruscate@[0m says this is a test of the line wrapping code @[0;7mtye@[0m says this is a test @[0;7mtye@[0m says ThisIsATestOfTheEmergencyBroadcastSystemIfThisHadBe +enAnActualEmergency... @[0;7mCoruscate@[0m says this is a test of the line wrapping code @[0;7mCoruscate@[0m says it works! @[0;7mtye@[0m says this is a test @[0;7mtye@[0m says ThisIsATestOfTheEmergencyBroadcastSystemIfThisHadBe +enAnActualEmergency... ThisIsATestOfTheEmergencyBroadcastSystemIfThisHadBeenAnActualEmergency +...
and the output:
Wrapping: @[0;7mCoruscate@[0m says this is a test of the line wrapping code Loose code: @[0;7mCoruscate@[0m says this is a test of the line wrapping code Tight code: @[0;7mCoruscate@[0m says this is a test of the line wrapping code Wrapping: @[0;7mtye@[0m says this is a test Loose code: @[0;7mtye@[0m says this is a test Tight code: @[0;7mtye@[0m says this is a test Wrapping: @[0;7mtye@[0m says ThisIsATestOfTheEmergencyBroadcastSystemIfThisHadBe +enAnActualEmergency... Loose code: @[0;7mtye@[0m says ThisIsATe stOfTheEmergencyBroadcastSystemIfThisHadBeenAnActualEmergency... Tight code: @[0;7mtye@[0m says ThisIsATe stOfTheEm ergencyBr oadcastSy stemIfThi sHadBeenA nActualEm ergency.. . Wrapping: @[0;7mCoruscate@[0m says this is a test of the line wrapping code @[0;7mCoruscate@[0m says it works! @[0;7mtye@[0m says this is a test @[0;7mtye@[0m says ThisIsATestOfTheEmergencyBroadcastSystemIfThisHadBe +enAnActualEmergency... Loose code: @[0;7mCoruscate@[0m says this is a test of the line wrapping code @[0;7mCoruscate@[0m says it works! @[0;7mtye@[0m says this is a test @[0;7mtye@[0m says ThisIsATe stOfTheEmergencyBroadcastSystemIfThisHadBeenAnActualEmergency... Tight code: @[0;7mCoruscate@[0m says this is a test of the line wrapping code @[0;7mCoruscate@[0m says it works! @[0;7mtye@[0m says this is a test @[0;7mtye@[0m says ThisIsATe stOfTheEm ergencyBr oadcastSy stemIfThi sHadBeenA nActualEm ergency.. . Wrapping: ThisIsATestOfTheEmergencyBroadcastSystemIfThisHadBeenAnActualEmergency +... Loose code: ThisIsATe stOfTheEmergencyBroadcastSystemIfThisHadBeenAnActualEmergency... Tight code: ThisIsATe stOfTheEm ergencyBr oadcastSy stemIfThi sHadBeenA nActualEm ergency.. .

In reply to Re^2: No \G for s///g ? (oops) by tye
in thread No \G for s///g ? by tye

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 admiring the Monastery: (6)
As of 2024-03-28 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found