将字符串拆分为块(不同大小)而不会破坏单词

我正在尝试创建一个方法,给定一个字符串,返回三个字符串:title,description1,description2

这是我发现的一个相关问题:将一个字符串拆分成指定大小的块而不会破坏单词 – 但我的块大小不同。

标题最多需要25个字符。

Description1需要最多35个字符。

Description2最多需要35个字符。

问题是:

如何拆分字符串以便最多创建三个实体( 注意 :如果字符串只能容纳第一个实体,那么我不需要返回三个实体),其中第一个实体具有最大值25个字符,另外两个最多35个字符。 使该方法足够聪明,可以考虑单词(也可能是标点符号),这样它就不会返回剪切结果。

我做了以下事情:

def split_text_to_entities(big_string) title = big_string(0..24) description1 = big_string(25..59) description2 = big_string(60..94) end 

但这种方法的问题在于,如果输入是“从我们的商店购买我们的新品牌鞋。城里最优惠的折扣和首次购买的40%折扣。”,结果将是:

 title = "Buy our new brand shoes f" description1 = "rom our store. Best discounts in to" description2 = "wn and 40% off for first purchase." 

理想情况下他们会:

 title = "Buy our new brand shoes" description1 = "from our store. Best discounts in" description2 = "town and 40% off for first" 

所以,考虑到单词,尝试按字符大小分割。

这不是诀窍吗?:

 def get_chunks(str, n = 3) str.scan(/^.{1,25}\b|.{1,35}\b/).first(n).map(&:strip) end 

为了涵盖所有基础,我会做以下事情。

 def divide_text(str, max_chars) max_chars.map do |n| str.lstrip! s = str[/^.{,#{n}}(?=\b)/] || '' str = str[s.size..-1] s end end 

(?=\b)是一个(零宽度)正向前瞻,它与一个单词中断相匹配,即一个空格字符或字符串的结尾。

例子

 max_nbr_chars = [25,35,35] str = "Buy our new brand shoes from our store. Best discounts in " + "town and 40% off for first purchase." divide_text(str, max_nbr_chars) #=> ["Buy our new brand shoes", # "from our store. Best discounts in", # "town and 40% off for first"] str = "Buy our new brand shoes from our store." divide_text(str, max_nbr_chars) #=> ["Buy our new brand shoes", "from our store.", ""] str = "Buy our new" divide_text(str, max_nbr_chars) #=> ["Buy our new", "", ""] str = "" divide_text(str, max_nbr_chars) #=> ["", "", ""] str = "Buyournewbrandshoesfromourstore." divide_text(str, max_nbr_chars) #=> ["", "Buyournewbrandshoesfromourstore.", ""] str = "Buyournewbrandshoesfromourstoreandshoesfromourstore." divide_text(str, max_nbr_chars) #=> ["", "", ""] 

请注意,如果从正则表达式中省略了^

 str = "Buyournewbrandshoesfromourstore." divide_text(str, max_nbr_chars) #=> ["ewbrandshoesfromourstore.", "rstore.", ""] 
 s = "Buy our new brand shoes from our store. Best discounts in town and 40% off for first purchase." s =~ /\b(.{,25})\W+(.{,35})\W+(.{,35})\b/ [$1, $2, $3] # => # [ # "Buy our new brand shoes", # "from our store. Best discounts in", # "town and 40% off for first purchase" # ]