# Create a string manipluation that replaces the 4th the last space with the expression xxx

**URL:** <https://forum.knime.com/t/create-a-string-manipluation-that-replaces-the-4th-the-last-space-with-the-expression-xxx/80480>\
**Category:** KNIME Extensions\
**Tags:** regex, text-processing\
**Created:** [June 18, 2024, 9:26am UTC](https://forum.knime.com/t/create-a-string-manipluation-that-replaces-the-4th-the-last-space-with-the-expression-xxx/80480 "2024-06-18T09:26:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![miriamgurk](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@miriamgurk](https://forum.knime.com/u/miriamgurk)\
**Post date:** [June 18, 2024, 9:26am UTC](https://forum.knime.com/t/create-a-string-manipluation-that-replaces-the-4th-the-last-space-with-the-expression-xxx/80480/1 "2024-06-18T09:26:52Z")

</div>

Hi Everyone.  
I tried to create a string manipulation for knime that replace the 4th last space of my string with the expression “xxx” however it didn’t work.  
Do you have a solution for this or an idea what I did wrong?

regexReplace($column1$, “(._?\s)(?:(?:._?\s){3})(.\*)”, “$1 x xx $2”)

many thanks in advance  
Miriam

---

<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 18, 2024, 9:58am UTC](https://forum.knime.com/t/create-a-string-manipluation-that-replaces-the-4th-the-last-space-with-the-expression-xxx/80480/2 "2024-06-18T09:58:18Z")

</div>

Hi @miriamgurk , welcome to the KNIME forum.

A couple of quick tips: when you repost you regex expression, it is best to highlight it and press the `</>` (preformatted text) button on the toolbar  
 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/5/4/545e4abc001cce2daa89be220355c7f2cf03fdab.png)

That way the forum software won’t try to interpret/modify the expression and we’ll be able to see and test _exactly_ what you have written.

Also it is useful to share sample data and expected output as well as writing the description, to ensure our interpretation of what is being asked is correct.

You could try this expression:

`regexReplace($column1$,"(.*)[]{1}((?:[^]*[]{1}){3})","$1xxx$2")`

![image](https://forum-cdn.knime.com/uploads/default/original/3X/2/e/2e59005564fa591c3f1b52bcb87dbc976facf85d.png)

---

<div class="post-metadata">

**Author:** ![gonhaddock](https://forum-cdn.knime.com/user_avatar/forum.knime.com/gonhaddock/32/35425_2.png) [@gonhaddock](https://forum.knime.com/u/gonhaddock)\
**Post date:** [June 18, 2024, 10:00am UTC](https://forum.knime.com/t/create-a-string-manipluation-that-replaces-the-4th-the-last-space-with-the-expression-xxx/80480/3 "2024-06-18T10:00:42Z")

</div>

Hello @miriamgurk and welcome to the KNIME community.  
It isn’t fully clear to me your challenge. From description I can assume that your text string has always four spaces, aiming to replace the fourth of them with `xxx`

You can test with the following code:

```auto
regexReplace($text$, "(.*)\\s(.*)", "$1***$2")

```

Otherwise, can you provide a text sample and expected output?

BR

---

<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 18, 2024, 10:04am UTC](https://forum.knime.com/t/create-a-string-manipluation-that-replaces-the-4th-the-last-space-with-the-expression-xxx/80480/4 "2024-06-18T10:04:13Z")

</div>

As per @gonhaddock’s comment, after posting I realised that I wasn’t quite sure what is supposed to happen if you have several spaces in succession e.g.

```auto
This string contains many consecutive spaces

```

and I also handled only space, and not other “whitespace”, so again you may need to be more specific for these “edge” cases.

---

<div class="post-metadata">

**Author:** ![miriamgurk](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@miriamgurk](https://forum.knime.com/u/miriamgurk)\
**Post date:** [June 18, 2024, 12:08pm UTC](https://forum.knime.com/t/create-a-string-manipluation-that-replaces-the-4th-the-last-space-with-the-expression-xxx/80480/5 "2024-06-18T12:08:38Z")

</div>

Thanks a lot everyone! This was exactly what I was looking for! 🙂

---

<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:** [September 16, 2024, 12:08pm UTC](https://forum.knime.com/t/create-a-string-manipluation-that-replaces-the-4th-the-last-space-with-the-expression-xxx/80480/6 "2024-09-16T12:08:59Z")

</div>

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