如何使用正则表达式添加换行符?

时间:2022-10-09 21:20:48

I have a really long string. I would like to add a linefeed every 80 characters. Is there a regular expression replacement pattern I can use to insert "\r\n" every 80 characters? I am using C# if that matters.

我有一个非常长的字符串。我想每80个字符添加一个换行符。是否有正则表达式替换模式我可以用来每80个字符插入“\ r \ n”?如果重要的话,我正在使用C#。

I would like to avoid using a loop.

我想避免使用循环。

I don't need to worry about being in the middle of a word. I just want to insert a linefeed exactly every 80 characters.

我不需要担心一言不发。我只想每80个字符插入一次换行。

1 个解决方案

#1


I don't know the exact C# names, but it should be something like

我不知道确切的C#名称,但它应该是类似的

str.Replace("(.{80})", "$1\r\n");

The idea is to grab 80 characters and save it in a group, then put it back in (I think "$1" is the right syntax) along with the "\r\n".

想法是抓取80个字符并将其保存在一个组中,然后将其重新放入(我认为“$ 1”是正确的语法)以及“\ r \ n”。

(Edit: The original regex had a + in it, which you definitely don't want. That would completely eliminate everything except the last line and any leftover pieces--a decidedly suboptimal result.)

(编辑:原始的正则表达式中有一个+,你绝对不需要。这将完全消除除最后一行和剩余部分之外的所有内容 - 一个明显不理想的结果。)

Note that this way, you will most likely split inside words, so it might look pretty ugly.

请注意,通过这种方式,您很可能会在单词内部进行拆分,因此看起来很丑陋。

You should be looking more into word wrapping if this is indeed supposed to be readable text. A little googling turned up a couple of functions; or if this is a text box, you can just turn on the WordWrap property.

如果这确实应该是可读文本,你应该更多地考虑自动换行。一个小小的谷歌搜索出现了几个功能;或者如果这是一个文本框,您只需打开WordWrap属性即可。

Also, check out the .Net page at regular-expressions.info. It's by far the best reference site for regexes that I know of. (Jan Goyvaerts is on SO, but nobody told me to say that.)

另外,请查看regular-expressions.info上的.Net页面。到目前为止,这是我所知道的正则表达式的最佳参考站点。 (Jan Goyvaerts是SO,但是没有人告诉我这么说。)

#1


I don't know the exact C# names, but it should be something like

我不知道确切的C#名称,但它应该是类似的

str.Replace("(.{80})", "$1\r\n");

The idea is to grab 80 characters and save it in a group, then put it back in (I think "$1" is the right syntax) along with the "\r\n".

想法是抓取80个字符并将其保存在一个组中,然后将其重新放入(我认为“$ 1”是正确的语法)以及“\ r \ n”。

(Edit: The original regex had a + in it, which you definitely don't want. That would completely eliminate everything except the last line and any leftover pieces--a decidedly suboptimal result.)

(编辑:原始的正则表达式中有一个+,你绝对不需要。这将完全消除除最后一行和剩余部分之外的所有内容 - 一个明显不理想的结果。)

Note that this way, you will most likely split inside words, so it might look pretty ugly.

请注意,通过这种方式,您很可能会在单词内部进行拆分,因此看起来很丑陋。

You should be looking more into word wrapping if this is indeed supposed to be readable text. A little googling turned up a couple of functions; or if this is a text box, you can just turn on the WordWrap property.

如果这确实应该是可读文本,你应该更多地考虑自动换行。一个小小的谷歌搜索出现了几个功能;或者如果这是一个文本框,您只需打开WordWrap属性即可。

Also, check out the .Net page at regular-expressions.info. It's by far the best reference site for regexes that I know of. (Jan Goyvaerts is on SO, but nobody told me to say that.)

另外,请查看regular-expressions.info上的.Net页面。到目前为止,这是我所知道的正则表达式的最佳参考站点。 (Jan Goyvaerts是SO,但是没有人告诉我这么说。)