# Replace or remove a portion of string with a regex

**URL:** <https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336>\
**Category:** KNIME Analytics Platform\
**Created:** [September 14, 2017, 5:47pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336 "2017-09-14T17:47:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gujodm](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/g/d26b3c/32.png) [@gujodm](https://forum.knime.com/u/gujodm)\
**Post date:** [September 14, 2017, 5:47pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/1 "2017-09-14T17:47:06Z")

</div>

Hi collegues,

I have this string composed by hour, minute and seconds, here an example:&nbsp; **17:24:55**

And I would like to remove the part&nbsp;after the : character and maintain only the hour part, as in this result output: **17**

So I have thought about using the **string replacer node** or the **string manipulation node,** but I don't have a clear idea of how to build a regex for remove or replace every characters after the :

Someone has an idea? Thanks in advice.

&nbsp;

---

<div class="post-metadata">

**Author:** ![James\_Davidson](https://forum-cdn.knime.com/user_avatar/forum.knime.com/james_davidson/32/70_2.png) [@James\_Davidson](https://forum.knime.com/u/James_Davidson)\
**Post date:** [September 15, 2017, 11:41am UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/2 "2017-09-15T11:41:23Z")

</div>

Hi,

If you are working with times, then I would recommend using the Date&Time nodes instead.&nbsp; However, regarding regex approaches - there are numerous ways to do this in KNIME.&nbsp; The attached workflow gives an example of using the Date&Time nodes, as well as using regex in the String Replacer node or the Regex Split node.

Kind regards

James

- [Extract hours.knwf](https://www.knime.com/sites/default/files/Extract%20hours.knwf)

---

<div class="post-metadata">

**Author:** ![MH](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@MH](https://forum.knime.com/u/MH)\
**Post date:** [September 15, 2017, 12:12pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/3 "2017-09-15T12:12:23Z")

</div>

String Replacer Node should work fine.

If you use the "regular expression" setting and enter:

**^(\d{2})\:.\***

as a pattern. The braces around the **\d{2}** (searching for two digits at the beginning of the string)  
define the pattern as a capturing group. Everything inside a capturing group can be extracted by a back reference.

If you enter **$1** (which is the back reference to the braces) into field Replacement Text, you should keep only the  
hour part of the string. Hope that helps.

---

<div class="post-metadata">

**Author:** ![gujodm](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/g/d26b3c/32.png) [@gujodm](https://forum.knime.com/u/gujodm)\
**Post date:** [September 18, 2017, 6:43pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/4 "2017-09-18T18:43:06Z")

</div>

Thank you MH@B,

your solution works perfectly!

---

<div class="post-metadata">

**Author:** ![gujodm](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/g/d26b3c/32.png) [@gujodm](https://forum.knime.com/u/gujodm)\
**Post date:** [September 18, 2017, 7:35pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/5 "2017-09-18T19:35:51Z")

</div>

Hi James, thank you for your reply and for your help,

unfortunately I cannot download the file with that extension... did you try&nbsp;to download it too for test if the link works correctly?

-Giulio

---

<div class="post-metadata">

**Author:** ![Goldeneye83](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/g/ac91a4/32.png) [@Goldeneye83](https://forum.knime.com/u/Goldeneye83)\
**Post date:** [March 12, 2020, 5:50pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/6 "2020-03-12T17:50:15Z")

</div>

Hi there. Similar problem here but not related to date/time.  
I’d like to remove the characters following a square close bracket.

For instance, I have this:  
[PP13, 112, W, ?, ?]#641

and would like to end up with:  
[PP13, 112, W, ?, ?]

I cant just drop the last 4 characters as the amount of characters following the “]” varies between rows.

I thought I could do it with an asterisk as wild card following a square bracket but it just takes out the bracket and leaves everything else:  
 ![image](https://forum-cdn.knime.com/uploads/default/original/2X/7/7ed0020e5eb0cd4bf11ad2c032971a6e8761c410.png)

So that’s a no-go.

Hope this isn’t threadjacking, just thought the right experience was already in this thread.

---

<div class="post-metadata">

**Author:** ![ipazin](https://forum-cdn.knime.com/user_avatar/forum.knime.com/ipazin/32/806_2.png) [@ipazin](https://forum.knime.com/u/ipazin)\
**Post date:** [March 12, 2020, 6:26pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/7 "2020-03-12T18:26:41Z")

</div>

Hi there @Goldeneye83,

you can use following expression in [String Manipulation](https://kni.me/n/eOn8ibZnxIkY7XhT) node using _substr()_ function:

`substr($column1$, 0, indexOf($column1$, "]") + 1)`

To use regex this one should work fine:

`regexReplace($column1$, "[^\\]]*$" ,"")`

Br,  
Ivan

---

<div class="post-metadata">

**Author:** ![Goldeneye83](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/g/ac91a4/32.png) [@Goldeneye83](https://forum.knime.com/u/Goldeneye83)\
**Post date:** [March 13, 2020, 12:56pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/8 "2020-03-13T12:56:18Z")

</div>

Thanks @ipazin that should work like a charm

---

<div class="post-metadata">

**Author:** ![andreluis](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@andreluis](https://forum.knime.com/u/andreluis)\
**Post date:** [December 18, 2020, 12:24am UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/9 "2020-12-18T00:24:26Z")

</div>

Hi everyone!

I have a similar problem, but it has been impossible to solve even the solutions mentioned here.

I’d like to exclude the last 3 characters from the character “B” considering the amount of characteres BEFORE the “B” varies in each rows. For example:

Data Set

78957389BP1  
789573890BP1  
789573BP3

Aim:

78957389  
789573890  
789573

Could you please help me!?

Many thanks.

---

<div class="post-metadata">

**Author:** ![ipazin](https://forum-cdn.knime.com/user_avatar/forum.knime.com/ipazin/32/806_2.png) [@ipazin](https://forum.knime.com/u/ipazin)\
**Post date:** [December 18, 2020, 9:03am UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/10 "2020-12-18T09:03:06Z")

</div>

Hello @andreluis,

and welcome to KNIME Community!

Here are two options that might work for you:

- remove last 3 characters from a string

`substr($col_name$, 0, length($col_name$) - 3)`

- remove everything after (last) character B in a string:

`regexReplace($col_name$, "(.*)B.*" , "$1")`

Br,  
Ivan

---

<div class="post-metadata">

**Author:** ![andreluis](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@andreluis](https://forum.knime.com/u/andreluis)\
**Post date:** [December 18, 2020, 2:35pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/11 "2020-12-18T14:35:38Z")

</div>

Many thanks!!! @ipazin

---

<div class="post-metadata">

**Author:** ![andrea\_ramirez](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/c0e974/32.png) [@andrea\_ramirez](https://forum.knime.com/u/andrea_ramirez)\
**Post date:** [October 14, 2021, 3:30am UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/12 "2021-10-14T03:30:01Z")

</div>

Hi, I have the following data set

8/3/21 8:15 PM

I need to keep the date only and get rid of the rest, as follows:

8/3/21

I tried a String manipulation

strip(substr($Resolution TimeStamp$,0,7))

But some dates have an additional digit like 10/10/21 and if I increase the amount of digits on the string manipulation I end up bringing an additional digit for the ones that have only 6 digits on the date.

Any ideas?

---

<div class="post-metadata">

**Author:** ![HansS](https://forum-cdn.knime.com/user_avatar/forum.knime.com/hanss/32/6979_2.png) [@HansS](https://forum.knime.com/u/HansS)\
**Post date:** [October 14, 2021, 5:09am UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/13 "2021-10-14T05:09:16Z")

</div>

Hi @andrea_ramirez  
Welcome to KNIME Forum!

If you extend your String Manipulation with the index function to identify the occurrence of the first " " (whitespace) your are done.  
`substr($column1$, 0 , indexOf($column1$," " ) )`  
gr. Hans

---

<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:** [October 14, 2021, 6:56am UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/14 "2021-10-14T06:56:41Z")

</div>

Hi @andrea_ramirez,

Welcome to the community. I know that this thread was about String Manipulation, but given its age, it probably would be best in future to open a new question, whilst maybe referring to the old post if you consider it heavily related.

Re your specific question, whilst you can do this with string manipulation, in general converting string data from one date/time format to another, such is what you are doing here, the safest option which doesn’t require any coding is to convert the string to date and time using the _String to Date&time_ node, specifying the format of your _existing_ data, and then convert back from date/time to string using the _Date&time to String_ node specifying the required new format.

So here you would convert to Datetime using a format such as:

d/M/yy h:mm a

(note the case of the letters used here as this is important to their meaning)

and then convert back to String using the format:

d/M/yy

One caveat and slight complication here is that the accepted capitalisation of the AM/PM has changed (possibly locale dependant) with the latest versions of java, and therefore can trip you up. For further info if this causes you problems, see this thread…

> [@String to Date&Time worked on previous knime version but having issues with current version (4.4.1)](https://forum.knime.com/t/string-to-date-time-worked-on-previous-knime-version-but-having-issues-with-current-version-4-4-1/36507):
>
> Hi there, I had used the string to Date&Time conversion on the previous Knime version and it had worked without issue. However it is not working on the new version - please see configuration → String is set as date-month-year hour:minutes AM/PM However I get the below error message now and I can’t figure what’s wrong with the setup “ERROR String to Date&Time 4:18:265 Execute failed: Failed to parse date in row 'Row0: Text ‘16/09/2021 6:07 PM’ could not be parsed at index 16” W…

---

<div class="post-metadata">

**Author:** ![ipazin](https://forum-cdn.knime.com/user_avatar/forum.knime.com/ipazin/32/806_2.png) [@ipazin](https://forum.knime.com/u/ipazin)\
**Post date:** [October 14, 2021, 11:19am UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/15 "2021-10-14T11:19:12Z")

</div>

Hello @andrea_ramirez,

there is also [Modify Time](https://kni.me/n/wQEjdwznaVGFw0MW) node which can append, change and remove time from Date&Time column.

Welcome to Community!

Br,  
Ivan

---

<div class="post-metadata">

**Author:** ![andrea\_ramirez](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/c0e974/32.png) [@andrea\_ramirez](https://forum.knime.com/u/andrea_ramirez)\
**Post date:** [October 14, 2021, 3:24pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/16 "2021-10-14T15:24:55Z")

</div>

This totally worked! thank you so much … I’ll keep moving on with my flow and get back for help if needed!

---

<div class="post-metadata">

**Author:** ![jarviscampbell](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/j/bbce88/32.png) [@jarviscampbell](https://forum.knime.com/u/jarviscampbell)\
**Post date:** [July 21, 2022, 2:47pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/18 "2022-07-21T14:47:33Z")

</div>

regexReplace($Name$, “[+^\[+Disabled+^\]]”, “”)

Hello there,

sorry about reopening this.

I am trying to remove the following simple expression from hundreds of rows (where found):

**[Disabled] car apple train**

I was almost able to do it with the following expression:

_regexReplace($Name$, “[+^\[+Disabled+^\]]”, “”)_

\*\* car apple train\*\*

but left me with the two spaces

Then I got this expression from [RegExr: Learn, Build, & Test RegEx](https://regexr.com/) where it matched perfectly, on the site only:

**\< [Disabled]\*\s\s \>** and added to expression:

_regexReplace($Name$, "[Disabled]_\s\s", “”)\*

and nothing has changed.

What am I doing wrong? Why can’t the regex shown on the regex site that matches work on Knime?

@ipazin

Thanks in advance.  
J.

---

<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:** [July 21, 2022, 5:28pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/19 "2022-07-21T17:28:21Z")

</div>

Not sure why you have 2 spaces but this should work fine  
[Disabled]\s+  
Be aware that some knime nodes need double backslashes when configuring.  
br

---

<div class="post-metadata">

**Author:** ![jarviscampbell](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/j/bbce88/32.png) [@jarviscampbell](https://forum.knime.com/u/jarviscampbell)\
**Post date:** [July 21, 2022, 6:20pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/20 "2022-07-21T18:20:22Z")

</div>

Hi @Daniel_Weikert , thanks for your reply

this \*\*

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/a/b/abd83e7fe61dbe8f7f7e3aba929d885165e05ae1.png)  
\*\*  
didn’t work. Result:

**[Dabled] car apple train**

removing the quotes, errored and didn’t let me even close the window.

Rectifying my last post when I mentioned this:

**regexReplace($Name$, “[+^\[+Disabled+^\]]”, “”)** had partially worked

It did not. In fact, it made worse, it went from this:

![image](https://forum-cdn.knime.com/uploads/default/original/3X/6/9/6946202eab6904bcf922172fa8211ee3fefc7150.png)

to this:

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

One last thing. I am super confused that it seems regex from any website I can match the expression I am trying to remove, when passing it over to Knime, it does not behave the same. In fact, doesn’t even like it at all. What is the difference between regex ‘versions’ if that’s even a thing?

---

<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:** [July 22, 2022, 4:00pm UTC](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336/21 "2022-07-22T16:00:46Z")

</div>

You need to escape your regex in the string manipulation node that way

 ![save](https://forum-cdn.knime.com/uploads/default/original/3X/1/7/17550d41fe882c9206b58bdf69197c45251a0bc5.png)

![res](https://forum-cdn.knime.com/uploads/default/original/3X/a/6/a67fb8cdf30faa7219d8c096e5de83fcbae6923b.png)

br

[Next page](https://forum.knime.com/t/replace-or-remove-a-portion-of-string-with-a-regex/9336.md?page=2)
