regex - Split string based on count and words using C# -


i need split string on words , each line should have 25 characters. example:

string original_text = "please write program breaks text small chucks. each chunk should have maximum length of 25 "

output should be:

"please write program",

"that breaks text",

"into small chucks. each",

"chunk should have a",

"maximum length of 25"

i tried using substring - breaking words

"please write program th" - wrong

"please write program" - correct

please write program - 23 characters, can take more 2 characters break word that.

string[] splitsamparr = splitsamp.split(',', '.', ';'); string[] mytext = new string[splitsamparr.length + 1];  int = 0; foreach (string splitsamparrval in splitsamparr) {     if (splitsamparrval.length > 25)     {         mytext[i] = splitsamparrval.substring(0, 25);         i++;     }     mytext[i] = splitsamparrval;      i++; } 

you can achieve with:

@"(\b.{1,25})(?:\s+|$)" 

see regex demo

this regex matches , captures group 1 character newline (with .) preceded word boundary (so, start matching whole words), 1 25 occurrences (thanks limiting quantifier {1,25}), , matches either 1 or more whitespace characters (with \s+) or end of string ($).

see code demo:

using system; using system.linq; using system.collections.generic; using system.text.regularexpressions; public class test {     public static void main()     {         var str = "please write program breaks text small chucks. each chunk should have maximum length of 25 ";         var chunks = regex.matches(str, @"(\b.{1,25})(?:\s+|$)")                  .cast<match>().select(p => p.groups[1].value)                  .tolist();         console.writeline(string.join("\n", chunks));     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -