开发者

How do I re-wrap a paragraph to a certain line length?

I have a big paragraph which I need to split into lines such that each line must not have more than 100 characters and no w开发者_开发百科ords must be broken. How would I go about doing this? I guess with regular expressions is the best way but I'm not sure how.


Use Text::Wrap.

Text::Wrap::wrap() is a very simple paragraph formatter. It formats a single paragraph at a time by breaking lines at word boundaries. Indentation is controlled for the first line ($initial_tab) and all subsequent lines ($subsequent_tab) independently.


While you should use a library function if you have one, as KennyTM suggested, a simple regex to solve this can be:

.{1,100}\b

This will take 100 characters or less, and will not break words. It would break other characters though, for example the period at the end of a sentence may be parted from the last word (last word<\n>. new line).
If that's an issue, you can also try:

.{1,99}(\s|.$)

That assures the last character in every match is a white space.

All of these assume you count spaces as characters, and probably don't have newlines in your text (a single paragraph), and don't have word of over 100 characters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜