Short answer - this worked for me:

s/(.{50,80})(\s+)/$1\n/g

Longer answer. I generated text containing random Cyrillic characters (simulating words of random length). I then split it into smaller, more easily readable, parts. Here's the test code:

#!/usr/bin/env perl -l use strict; use warnings; use open OUT => qw{:encoding(utf8) :std}; # From: http://www.unicode.org/charts/PDF/U0400.pdf # Cyrillic: 0400 - 04FF # Basic Russian alphabet: 0410 - 044F my @sample_chars = map { chr } hex '0410' .. hex '044F'; print 'Total characters for random selection: ', 0+@sample_chars; my $string; for (0 .. 50) { for (1 .. 5 + rand 5) { $string .= $sample_chars[rand @sample_chars]; } $string .= ' '; } print 'One line:'; print $string; (my $multiline_string = $string) =~ s/(.{50,80})(\s+)/$1\n/g; print 'Many lines:'; printf '%10s', $_ for 1 .. 8; print ''; print '1234567890' x 8; print $multiline_string

Here's the output from a sample run:

Total characters for random selection: 64
One line:
дэНштц ЗжывдЖеб АэиТорЫиФ хоИыбЫйси СбГВЧмп шзхендюх жэШЗмВ фЕЙяЛСрЛж виуПхьф жзиНж НщВЬУьы ЪЩОЬБсыон сфИоуМэы ТЬХуйххЯШ дъЬуйъ ЦОзкИ ЧЫйЯф НпГзэц ЭмЭЭВХННС иЧАктЭУЩ сктТфэ ъсязцл ЬОбФцю ХМуик ПбюНшсюЪю ЪнЯцмИТ сШЪьхесвт ейячж тРцеъвР эЙдщчфПИЫ шгдЫщйР йЙДЫлрЮс еХъкДьНюС АТажшкеЙ ПхюЩдйИОх хкваЭ еЙЫЮнФ яСеяеВСШ йюЬижНЧРв СвыЭШАн ЮтвсъХЦа ъРЭШЖВню чЩыоСЕц УУЪуЦЭ эЬЮШвХЧЗ щсущйМ ЩгБхщЛргА фыЯОНдм КзЕХВА фБсиыШ пжАьР 
Many lines:
         1         2         3         4         5         6         7         8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
дэНштц ЗжывдЖеб АэиТорЫиФ хоИыбЫйси СбГВЧмп шзхендюх жэШЗмВ фЕЙяЛСрЛж виуПхьф
жзиНж НщВЬУьы ЪЩОЬБсыон сфИоуМэы ТЬХуйххЯШ дъЬуйъ ЦОзкИ ЧЫйЯф НпГзэц ЭмЭЭВХННС
иЧАктЭУЩ сктТфэ ъсязцл ЬОбФцю ХМуик ПбюНшсюЪю ЪнЯцмИТ сШЪьхесвт ейячж тРцеъвР
эЙдщчфПИЫ шгдЫщйР йЙДЫлрЮс еХъкДьНюС АТажшкеЙ ПхюЩдйИОх хкваЭ еЙЫЮнФ яСеяеВСШ
йюЬижНЧРв СвыЭШАн ЮтвсъХЦа ъРЭШЖВню чЩыоСЕц УУЪуЦЭ эЬЮШвХЧЗ щсущйМ ЩгБхщЛргА
фыЯОНдм КзЕХВА фБсиыШ пжАьР 

I ran this several times with similar results. Lines only wrapped when they otherwise would have exceeded 80 characters (not 80 bytes). The 50 in {50,80} was somewhat arbitrary: vaguely based on the longest, non-technical word in English having 29 characters. I don't speak, read or write Russian: you can probably come up with a better number.

As you can see from the code, the only statement I needed to handle Unicode was for the output:

use open OUT => qw{:encoding(utf8) :std};

The problem on your command line examples is that you're not specifying the encoding to Perl: it just does its best and deals with the bytes you're feeding it. If you indicate the encoding, the '\b' assertion works fine and '.' matches characters:

$ echo "дэНштц ЗжывдЖеб АэиТорЫиФ хоИыбЫйси СбГВЧмп шзхендюх жэШЗмВ фЕЙяЛСрЛж виуПхьф" | perl -C -pe 's/(.{1,20})\b/$1\n/g'
дэНштц ЗжывдЖеб 
АэиТорЫиФ хоИыбЫйси 
СбГВЧмп шзхендюх 
жэШЗмВ фЕЙяЛСрЛж 
виуПхьф

— Ken


In reply to Re: making a regex work with Unicode by kcott
in thread making a regex work with Unicode by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.