简体   繁体   English

正则表达式将字符串拆分为非常大的块

[英]Regex to split string into verying size chunks

I'm trying to break a string into chunks of varying sizes, creating a "wave" of words such as: 我试图将一个字符串分成大小不同的块,从而产生一波“波”,例如:

          the
        cat sat
     on the mat the
  cat sat on the mat the
     cat sat on the
        mat the
          cat

As words have varying lengths I want to split on the nearest space. 由于单词的长度各不相同,因此我想在最近的空格处拆分。

I may be trying to do too much with one line. 我可能只想用一条线做太多事情。 However I don't like the idea of loops. 但是我不喜欢循环的想法。 I've started with this: 我从这里开始:

/.{5}\w*/g

I've tried adding () around and adding {} but can't quite get the hang of regexes. 我试过在周围加上(),再加上{},但不能完全理解正则表达式。 Is this possible to do? 这可能吗? Or will there involve some sort of loop? 还是会涉及某种循环?

I think there is something that can be done even with a regex, BUT a lot depends on the input string. 我认为即使使用正则表达式也可以完成某些操作,但很大程度上取决于输入字符串。 Also, you will have to think about how to arrange chunks, whether to trim or not, how to pad, etc. so just a regex won't do. 另外,您将不得不考虑如何排列块,是否修剪,如何填充等,​​因此仅使用正则表达式是不行的。

This regex pattern: ((?:[^\\s]+(?:\\s|$)){1,20}) can yield something similar to what you are looking for in the following string: the cat sat on the mat the cat sat on the mat the cat sat on the mat . 此正则表达式模式: ((?:[^\\s]+(?:\\s|$)){1,20})可以产生类似于您在以下字符串中寻找的东西: the cat sat on the mat the cat sat on the mat the cat sat on the mat It works because of additional spaces right where the breakdown should occur. 它之所以起作用,是因为应该在发生故障的位置附加了空格。

the 
cat sat 
on the mat the 
cat sat on the mat the 
cat sat on the 
mat

See demo . 参见演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM