I have this string composed by hour, minute and seconds, here an example: 17:24:55
And I would like to remove the part 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 :
If you are working with times, then I would recommend using the Date&Time nodes instead. However, regarding regex approaches - there are numerous ways to do this in KNIME. 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.
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.
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:
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.
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
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…
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:
to this:
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?