Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have a Russian article (Unicode) that I pasted into a plain-text file from a website, meaning each paragraph takes up one "line", and i'd like to be able to read it comfortably in a terminal, i.e. with wrapping on word boundaries instead of the teminal edge. fold does not handle the Unicode characters correctly - word boundaries are ignored and some characters are mangled by the line breaks. I then tried a perl one-liner that works fine with ASCII:
perl -pe's/(.{0,60})\b/$1\n/g' <text-fileThis produced output where most lines were not wrapped and the ones that were were before Latin characters, and both before and after numerals. I looked up perldoc regex and tried this:
perl -pe's/(.{1,60}\b)/$1\n/ug' <text-fileThis produced output that was wrapped variably between ~33-40 columns, depending on the number of spaces/Latin characters in the line (in other words, the . was couning bytes, not characters). Word boundaries were ignored. I tried many permutations of use utf8 and use feature "unicode_strings" and s///u and s///a and s///aa and \b{wb} but the result is always one of these two cases. What, if not anything I've tried so far, is the correct way to make . and \b work properly with Unicode, and if I am doing the "right thing", why isn't it working?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: making a regex work with Unicode
by haukex (Archbishop) on Feb 25, 2017 at 20:48 UTC | |
|
Re: making a regex work with Unicode
by poj (Abbot) on Feb 25, 2017 at 20:42 UTC | |
|
Re: making a regex work with Unicode
by kcott (Archbishop) on Feb 26, 2017 at 06:08 UTC | |
|
Re: making a regex work with Unicode
by Anonymous Monk on Feb 25, 2017 at 21:32 UTC | |
|
Re: making a regex work with Unicode
by vr (Curate) on Feb 26, 2017 at 10:15 UTC | |
|
Re: making a regex work with Unicode
by Anonymous Monk on Feb 25, 2017 at 20:49 UTC |