将字符串解析为指定的长度(不剪切单词)

时间:2022-04-18 02:35:24

I have a long string that I need to parse into an array of strings that do not exceed 40 characters in length. The tricky part of this for me is making sure that the regex finds the last whitespace before 40 characters to make a clean break between strings since I don't want words cut off.

我有一个长字符串,我需要解析成一个长度不超过40个字符的字符串数组。对我来说,这个棘手的部分是确保正则表达式找到40个字符之前的最后一个空格,以便在字符串之间进行干净的中断,因为我不希望单词被切掉。

2 个解决方案

#1


Right-trim the substrings as you go:

在你去的时候右边修剪子串:

(?<sub>.{1,40})(?:\s+|$)|(?<sub>.{40})

The first alternative tries for a clean break, but the other is there as a fallback for blindly chopping if need be. Afterward, the substrings are available in m.Groups["sub"].Captures.

第一种选择尝试干净的休息,但另一种选择是作为盲目砍伐的后备,如果需要的话。之后,子串在m.Groups [“sub”]中可用。捕获。

#2


This regex should do the job:

这个正则表达式应该做的工作:

".{1,40}( |$)"

(Quotes are for the string literal.)

(引用是字符串文字。)

This simply tells the regex parser to do a greedy match of any char between 1 and 40 times (i.e. as many as possible) before it then finds a single space (or the end of the string).

这简单地告诉正则表达式解析器在找到单个空格(或字符串的结尾)之前,在1到40次之间(即尽可能多的)执行任何char的贪婪匹配。

#1


Right-trim the substrings as you go:

在你去的时候右边修剪子串:

(?<sub>.{1,40})(?:\s+|$)|(?<sub>.{40})

The first alternative tries for a clean break, but the other is there as a fallback for blindly chopping if need be. Afterward, the substrings are available in m.Groups["sub"].Captures.

第一种选择尝试干净的休息,但另一种选择是作为盲目砍伐的后备,如果需要的话。之后,子串在m.Groups [“sub”]中可用。捕获。

#2


This regex should do the job:

这个正则表达式应该做的工作:

".{1,40}( |$)"

(Quotes are for the string literal.)

(引用是字符串文字。)

This simply tells the regex parser to do a greedy match of any char between 1 and 40 times (i.e. as many as possible) before it then finds a single space (or the end of the string).

这简单地告诉正则表达式解析器在找到单个空格(或字符串的结尾)之前,在1到40次之间(即尽可能多的)执行任何char的贪婪匹配。