Hello guys. I need to write an expression, which clean data. But at the end I run into problem “Ambiguous return type”
Expression:
if((regexMatches($Materialtext$, “.?(\d+x\d+).”)))
(regexReplace($Materialtext$, “.?(\d+x\d+).”, “$1”))
else
“”
Hello guys. I need to write an expression, which clean data. But at the end I run into problem “Ambiguous return type”
Expression:
if((regexMatches($Materialtext$, “.?(\d+x\d+).”)))
(regexReplace($Materialtext$, “.?(\d+x\d+).”, “$1”))
else
“”
Hello @Alex_025
You can test your code for ‘String Manipulation’ node with the following:
regexMatches($Materialtext$, ".?(\d+x\d+).").equals("True")
? regexReplace($Materialtext$, ".?(\d+x\d+).", "$1")
: ""
Alternatively you can test the following in a ‘Column Expressions’ node:
if(regexMatches($Materialtext$, ".?(\d+x\d+).").equals("True")){
regexReplace($Materialtext$, ".?(\d+x\d+).", "$1")
} else {
""
}
BR
Hi @Alex_025 , there are several “problems” that you need to resolve to make this work, but the good news is that it can be made to work in String Manipulation:
Condition statements in String Manipulation
First off, there is no “if - else” keyword in String Manipulation. What you can use, however is the “java ternary operator” syntax which takes the form
condition ? <value if true>
: <value if false>
Use of = in String Manipulation
The regexMatches () function returs “True” or “False” strings rather than booleans. To perform an equality check with these, you need to use the .equals() syntax because this is based on java, and = does not return the correct result when used in String comparisons
Regular expressions need to be double-escaped
Because the String Manipulation node attempts to parse the string, any escape characters \
need to be escaped themselves so that they get through to the underlying regex functions correctly.
So the condition becomes
regexMatcher($Materialtext$, ".?(\\d+x\\d+).").equals( "True")
?regexReplace($Materialtext$, ".?(\\d+x\\d+).", "$1")
:""
How to return it
The String Manipulation node doesn’t understand the syntax by itself, so it needs to be told that it has to return the result as a string
So your condition needs to become:
string(
regexMatcher($Materialtext$, ".?(\\d+x\\d+).").equals( "True")
?regexReplace($Materialtext$, ".?(\\d+x\\d+).", "$1")
:""
)
Ok, thank you. I tried your variant. But now the problem, thet it puts everywhere empty “”, enev if the information exists in a row
Hi @Alex_025 , that is because I simply copied your condition. I assumed that this is what you wanted to do, since that is what you wrote!
What do you want it to do?
e.g.
If you want it to return the value of $Materialtext$ then replace the ""
with $Materialtext$
Hello. In my case I need to cleen some data from useless text. Initially I had this simple expressionplace($: regexReMaterialtext$, “.?(\d+x\d+).”, “$1”), but if it doesn’t find a match, it writes some text. So I want to change the expression so that if nothing is found, leave the cell empty or write some custom text for all cases.
Hi @Alex_025 , Can you elaborate further on what you are specifically wanting to happen. When you say
“I want to change the expression so that if nothing is found, leave the cell empty or write some custom text for all cases.”
At the moment, it is leaving the cell empty. You can change it to write some custom text, but I don’t know what custom text you want.
Also, by “when nothing is found” do you mean when there is no match found, or just if the cell is empty?
In terms of answering your question (how to overcome the “ambiguous return type” and including conditions in String Manipulation, I feel your question has been answered. You weren’t asking about how to make your regex work, which is why I didn’t concern myself with asking you for sample data or an explanation of the regex pattern.
Just to note, the String Manipulation expression I gave above will produce the following:
If it is supposed to be doing something else, or if it isn’t working with your data then maybe the regular expression is wrong. You haven’t shown is any sample data and I have to assume the regular expression you gave is right, because that is all I have to go on.
The example data I show in the above screenshot is data I made up based on what I believe your regex is looking for. If it isn’t working for you, or you need further assistance with it, then you will need to offer up some sample data, and additional explanation.
This is the simple demo with my above data, if that helps…
String Manipulation Conditions.knwf (14.7 KB)
Hi @takbb,
wow. It is awesome to read that the String Manipulation node is capable of doing conditional logic.
With the Rule Engine being not able to output a concatenated string, I thought that a Column Expression (which is slower) or the Java Snippet (which is more complex) are the only alternatives. I think, this will impact the way I handle specific situations in my workflows.
Thanks for sharing. Also, it would be great to look info like this up in the node description.
Hi @JLD , I totally agree that it would be nice to see this officially documented within the node.
On that note, you may also be interested in the following knowledge sharing post I wrote last year.
Hi @takbb,
already found this gem
I wish, threads like this would be kept open longer so that they become a steady collection of new ideas/techniqes.
I also checked out your collection of components in the KNIME Hub. Good stuff, I can learn a lot from it. I think I will also upload some reusable components in the future.
Thanks for the kind words @JLD. I agree with you that it would be nice if the thread could be added to as a source of information, but what I do instead is [try to remember to ] add a link to that post whenever I post something related to it, so that it at least builds up references on that post to other material
e.g. at the end of the above post it gradually builds a list of related information/threads:
Hello,
I’ve just revisited the topic and realized about some inconsistencies in my first post; within both of the code cases. I would probably wrote without testing at that time
Then, aiming to amend myself, let me post the tested correct code syntaxes (where ""
can be replaced with null
depending expected output):
String Manipulation syntax:
regexMatcher($Materialtext$, ".+?(\\d+x\\d+).+").equals("True")
? regexReplace($Materialtext$, ".+?(\\d+x\\d+).+", "$1")
: null
'* watch for double bars
Column Expressions syntax:
if(regexMatcher(column("Materialtext"), ".+?(\\d+x\\d+).+") == true){
regexReplace(column("Materialtext"), ".+?(\\d+x\\d+).+", "$1")
} else {
null
}
'* watch for if
statement
I’m posting this workflow for checks:
20240307_reviewing_anbiguous_return_type_v0.knwf (17.1 KB)
BR
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.