# Regex Extractor - regex splits at different size as expected

**URL:** <https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794>\
**Category:** Community Extensions\
**Tags:** palladian-selenium\
**Created:** [June 16, 2021, 2:08pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794 "2021-06-16T14:08:08Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![IrynaK](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/i/13edae/32.png) [@IrynaK](https://forum.knime.com/u/IrynaK)\
**Post date:** [June 16, 2021, 2:08pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/1 "2021-06-16T14:08:08Z")

</div>

Hello,  
I have found this topic and followed the regex presented there: [Regex split to split the string without breaking the word](https://forum.knime.com/t/regex-split-to-split-the-string-without-breaking-the-word/23987)

I used in in the node Regex Extractor: (.{1,60}[^\s]\*)?\s? to split string at 60 char mark. However, it splits it at 66 or 68, depending on the length of the word instead of moving the last word to the splitted group.

Is there a change required to the Regex in order to impose the split at max 60 char?

---

<div class="post-metadata">

**Author:** ![Daniel\_Weikert](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@Daniel\_Weikert](https://forum.knime.com/u/Daniel_Weikert)\
**Post date:** [June 16, 2021, 4:32pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/2 "2021-06-16T16:32:06Z")

</div>

If possible always provide a sample file for others to help you out

---

<div class="post-metadata">

**Author:** ![IrynaK](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/i/13edae/32.png) [@IrynaK](https://forum.knime.com/u/IrynaK)\
**Post date:** [June 16, 2021, 4:34pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/3 "2021-06-16T16:34:37Z")

</div>

Need to split a string (eg. Bay PT C.P.R. Station Grounds being PT of locations 8 3C PIC SRO PT 4 & 5 55R2614 Except PT 1 55R3261 ) into multiple; first is max 40 char, next is max 40 char etc. whole words

---

<div class="post-metadata">

**Author:** ![takbb](https://forum-cdn.knime.com/user_avatar/forum.knime.com/takbb/32/57077_2.png) [@takbb](https://forum.knime.com/u/takbb)\
**Post date:** [June 16, 2021, 5:04pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/4 "2021-06-16T17:04:19Z")

</div>

Hi @IrynaK I’m not entirely certain that this can be done with pure regex. Your requirement is subtly different to the post you referenced.

What the regex pattern that you have used does is match up to the first 60 characters and then also capture everything after that up to but not including the next white space. This ensures it didn’t break mid-word. That’s why you see the results you are getting. It isn’t restricted to 60 characters. It returns 60 or so…

Does this absolutely have to be a regex solution? If it does then maybe an alternative is to give a guess at what the longest word you might encounter is likely to be and subtract that from 60, then use that number in your regex in place of the 60 value. So, say it gives you, say, “50 or so” instead…

Although in your latest comment you are talking about 40, so I’m now slightly confused about the requirement.but gleefully you can see what I’m saying.

Not saying it definitely can’t be done with regex but I can’t think of a way at the moment.

---

<div class="post-metadata">

**Author:** ![Daniel\_Weikert](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@Daniel\_Weikert](https://forum.knime.com/u/Daniel_Weikert)\
**Post date:** [June 16, 2021, 5:17pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/5 "2021-06-16T17:17:39Z")

</div>

Could you split by space and then divide into chunks a 40?  
(With sample we rather mean an actual file :))

---

<div class="post-metadata">

**Author:** ![danielesser](https://forum-cdn.knime.com/user_avatar/forum.knime.com/danielesser/32/331_2.png) [@danielesser](https://forum.knime.com/u/danielesser)\
**Post date:** [June 16, 2021, 5:20pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/6 "2021-06-16T17:20:30Z")

</div>

Hi @IrynaK,

As @takbb already pointed out, you are using a regular expression that splits input on the first whitespace **after** 40 characters. This leads to string lengths of \>= 40.

Just use this expression instead:

```auto
(.{1,40})

```

I also attached an example workflow to my NodePit Space:

> **[regex-extactor-regex-splits-at-different-size-as-expected-33794 \[Workflow\] —...](https://nodepit.com/workflow/com.nodepit.space/daniel/public/forum/regex-extactor-regex-splits-at-different-size-as-expected-33794.knwf)**
>
> There has been no description set for this workflow's metadata.

This already works out of the box. The example string split by 40 characters looks as follows… Just configure the [Regex Extractor](https://nodepit.com/node/ws.palladian.nodes.extraction.regex.RegexExtractorNodeFactory) to split matches in rows or columns as you prefer.

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/4/a/4adcdd165ed70b1da74cb47bdd51315f04bf0d4b.png)

Best regards,  
Daniel

---

<div class="post-metadata">

**Author:** ![takbb](https://forum-cdn.knime.com/user_avatar/forum.knime.com/takbb/32/57077_2.png) [@takbb](https://forum.knime.com/u/takbb)\
**Post date:** [June 16, 2021, 5:36pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/8 "2021-06-16T17:36:50Z")

</div>

OK, maybe I take back what I said about regex 😉

(.{1,40})\s.\*

Would collect up to 40 characters without breaking on the middle of a word… I think.

You can try that actual expression out as it’s saved on the following link…

> **[regex101: build, test, and debug regex](https://regex101.com/r/jBOT5f/1)**
>
> Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java. Features a regex quiz & library.

---

<div class="post-metadata">

**Author:** ![IrynaK](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/i/13edae/32.png) [@IrynaK](https://forum.knime.com/u/IrynaK)\
**Post date:** [June 16, 2021, 5:56pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/9 "2021-06-16T17:56:56Z")

</div>

> [@takbb](#):
>
> (.{1,40})\s.\*

Thank you very much for your help guys! (.{1,40}) seems to be working, but I will be testing the whole file later.

This forum is awesome and I cannot thank you enough 🙂

---

<div class="post-metadata">

**Author:** ![IrynaK](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/i/13edae/32.png) [@IrynaK](https://forum.knime.com/u/IrynaK)\
**Post date:** [June 16, 2021, 7:15pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/10 "2021-06-16T19:15:18Z")

</div>

> [@takbb](#):
>
> (.{1,40})\s.\*

So I ended up using (.{1,40})[\s.]\*\s  
it seems to split the string to max 40 or less with full words intact, however I am unable to catch the last part of the string with this regex. Can you help?

---

<div class="post-metadata">

**Author:** ![takbb](https://forum-cdn.knime.com/user_avatar/forum.knime.com/takbb/32/57077_2.png) [@takbb](https://forum.knime.com/u/takbb)\
**Post date:** [June 16, 2021, 10:56pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/11 "2021-06-16T22:56:40Z")

</div>

What happens if you put brackets around the second part… So you’d capture both. You might need to trim whitespace afterwards depending on your use case

e.g  
(.{1,40})([\s.]_\s)  
Or  
(.{1,40})([\s.]_)\s  
Or maybe…  
(.{1,40})\s\*([\s.]\*)

---

<div class="post-metadata">

**Author:** ![IrynaK](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/i/13edae/32.png) [@IrynaK](https://forum.knime.com/u/IrynaK)\
**Post date:** [June 16, 2021, 11:57pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/12 "2021-06-16T23:57:14Z")

</div>

> [@IrynaK](#):
>
> Bay PT C.P.R. Station Grounds being PT of locations 8 3C PIC SRO PT 4 & 5 55R2614 Except PT 1 55R3261

Unfortunately those did not work.

This formula works if I add a space at the end of the string: (.{0,40})[\s]  
So I am wondering how can I say that the last group needs to be captured from the end not the whitespace?

---

<div class="post-metadata">

**Author:** ![dnaki](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dnaki](https://forum.knime.com/u/dnaki)\
**Post date:** [June 17, 2021, 2:42am UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/13 "2021-06-17T02:42:25Z")

</div>

Hi.  
Building on the regexes above, how about this one: (.{1,40}\b)|(.{1,40}\b$)

As @takbb states, you may have to trim white space depending on your needs.  
There’s probably a way to tweak the regex to exclude the whitespace.

Hope this helps.  
-Don

---

<div class="post-metadata">

**Author:** ![IrynaK](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/i/13edae/32.png) [@IrynaK](https://forum.knime.com/u/IrynaK)\
**Post date:** [June 17, 2021, 1:44pm UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/14 "2021-06-17T13:44:42Z")

</div>

> [@dnaki](#):
>
> (.{1,40}\b)|(.{1,40}\b$)

Thank you, it seems to work!

---

<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:** [December 17, 2021, 1:44am UTC](https://forum.knime.com/t/regex-extractor-regex-splits-at-different-size-as-expected/33794/15 "2021-12-17T01:44:43Z")

</div>

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