I am working on knime with a string replacer node and have to add an mathematical operation sign (multiplication, '*') behind a digit number (within a string cell). I find the numbers with the (java-based) regular expression
((?:\d+\.)?\d+ )
I tried to add the multiplication operation by adding the replacement text
((?:\d+\.)?\d+)\*
But the replacement now creates something like:
to be replaced:
1.456 substance
actually replaced by:
((?:\d+\.)?\d+)\*substance
But needed is:
1.456*substance
How can I modify the replacement text to get the needed output or what is the proper approach?
Thank you.
Note that when you search for the text, you are using a regular expression pattern, and when replacing, you need a replacement string. The replacement string cannot contain the patterns, but it can contain back-references to the captured groups (numbered or named ones).
In Java, back-references are referenced to with $
+number
notation.
So, using ((?:\d+\.)?\d+)\s
regex, and a $1*
replacement string, you can get what you need: 1.456*substance
.
See your updated regex demo.
Note that to match float values, you may consider using (\d+(?:\.\d+)?)\s
or (\d*\.?\d+)\s
regex patterns. For a complex pattern to match all kinds of float numbers, see Matching Floating Point Numbers with a Regular Expression at regular-expressions.info.