# Regex split to split the string without breaking the word

**URL:** <https://forum.knime.com/t/regex-split-to-split-the-string-without-breaking-the-word/23987>\
**Category:** KNIME Analytics Platform\
**Created:** [May 21, 2020, 4:37pm UTC](https://forum.knime.com/t/regex-split-to-split-the-string-without-breaking-the-word/23987 "2020-05-21T16:37:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathi](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@mathi](https://forum.knime.com/u/mathi)\
**Post date:** [May 21, 2020, 4:37pm UTC](https://forum.knime.com/t/regex-split-to-split-the-string-without-breaking-the-word/23987/1 "2020-05-21T16:37:32Z")

</div>

I need to split the string if it exceeds certain length, but it should not break the word inside the string.  
For example:  
Sting\_output\_column= “knime analytical platform Split Regex is used for splitting the string into different rows”

I need to break the “Sting\_output\_column” into different rows of certain length(20). How to achieve it without breaking the word.

---

<div class="post-metadata">

**Author:** ![AnotherFraudUser](https://forum-cdn.knime.com/user_avatar/forum.knime.com/anotherfrauduser/32/16912_2.png) [@AnotherFraudUser](https://forum.knime.com/u/AnotherFraudUser)\
**Post date:** [May 21, 2020, 5:01pm UTC](https://forum.knime.com/t/regex-split-to-split-the-string-without-breaking-the-word/23987/2 "2020-05-21T17:01:59Z")

</div>

Hi @mathi,

I think the easiest and cleanest solution would be a Java Snippet instead of regex in this case.  
While I love regex, this problem is easier solved using a loop and having checks for newline/max character per line

Something like this:

> <https://stackoverflow.com/questions/7528045/large-string-split-into-lines-with-maximum-length-in-java>

I guess you could do the same in a KNIME Loop with split string to words

---

<div class="post-metadata">

**Author:** ![mathi](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@mathi](https://forum.knime.com/u/mathi)\
**Post date:** [May 21, 2020, 7:01pm UTC](https://forum.knime.com/t/regex-split-to-split-the-string-without-breaking-the-word/23987/3 "2020-05-21T19:01:04Z")

</div>

Earlier I had implemented my solution using java snippet, but thought of doing it using knime components instead of JAVA. will try to implement the WF using loop. Thanks!

---

<div class="post-metadata">

**Author:** ![armingrudd](https://forum-cdn.knime.com/user_avatar/forum.knime.com/armingrudd/32/84633_2.png) [@armingrudd](https://forum.knime.com/u/armingrudd)\
**Post date:** [May 21, 2020, 10:08pm UTC](https://forum.knime.com/t/regex-split-to-split-the-string-without-breaking-the-word/23987/5 "2020-05-21T22:08:39Z")

</div>

Hi @mathi,

You can use this regex in the Regex Split node:  
`(.{1,19}[^\s]*)?\s?`

The only issue here is that you need to repeat it enough times to let the longest string in your table be also a match.  
To overcome this issue, I have built a workflow which creates the pattern for the Regex Split node automatically:

 ![23987](https://forum-cdn.knime.com/uploads/default/original/3X/c/9/c970b06649bc59218df6938f267719743b183ece.jpeg)

[23987-1-1.knwf](https://forum.knime.com/uploads/short-url/8bNAgVMrFmvCSC4zLbvZEPsx1tH.knwf) (55.4 KB)

😊

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [November 20, 2020, 10:21am UTC](https://forum.knime.com/t/regex-split-to-split-the-string-without-breaking-the-word/23987/6 "2020-11-20T10:21:24Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
