Differentiate between hyphen [-] and negative sign [-2.22] - pentaho-spoon

I am working in Pentaho kettle version 8.2 and I have JSON Input. Here is extract from JSON.
{
"CEPS": "-6.58",
"PE": "-2.22",
"OPM": "-",
"NPM": "-",
}
In JSON input all columns set as string. So I want to replace value of OPM and NPM with 0. For this I am using "Replace in string" task which replace - with 0 but its replacing negative sign of PE. So can anyone suggest me Regex which fulfill my requirement.

how about not using regex and just set like
[find string] [change string]
"OPM": "-" → "OPM" : "0"
"NPM": "-" → "NPM" : "0"
in case you change only 2 key (OPM, NPM)

Related

Find the documents which contains a specific value in a array

I want to find documents that contain a single - (symbol).
occupationalCategory array consists of single - (symbol) instead of a double on specific employerId.
wrongly inserted with (single - symbol)
"occupationalCategory" : [
"15-1132.00 - Software Developers, Applications"
],
its should be : (double -- symbol)
"occupationalCategory" : [
"15-1132.00 -- Software Developers, Applications"
]
Please help me to get those documents.
As you mentioned that the string pattern is consistent, you can use regex to match the string pattern.
^\d+-\d+.\d{2} - [\w\s]+, \w+$
^ - Start with
\d - Match with digit
+ - Refer previous character/symbol with at least 1 occurrence
- - - Symbol
\d+.\d{2} - Match for decimal pattern
\w - Word character
\s - Spacing character
$ - Match End
Sample Regex 101 & Test Output
db.collection.find({
"occupationalCategory": {
$regex: "\\d+-\\d+.\\d{2} - [\\w\\s]+, \\w+",
$options: "m"
}
})
Sample Mongo Playground

In Couchbase Java Query DSL, how do I filter for field-values that are not ASCII?

Using Couchbase Java DSL, a query using "fish/piraña" gives a parse-error, but with "fish/piranha", there is no parse-error.
I had thought that the x() method would correctly wrap the non-ASCII Unicode string.
Using N1ql directly, this does work with any field name (except blank) or field value:
parameterized("SELECT * from" + bucket.name() + "WHERE" + fieldName + "= $v", placeholders))
How can this be done using the Java Query DSL?
String species "fish/pira\u00f1a" ;
Expression expForType = x("species").eq(x(species));
OffsetPath statement = select("*").from(i(bucket.name())).where(expForType);
N1qlQuery q = N1qlQuery.simple(statement);
N1qlQueryResult result = bucket.query(q);
So, it works via N1QL:
N1qlParams params = N1qlParams.build().consistency(ScanConsistency.REQUEST_PLUS).adhoc(true);
ParameterizedN1qlQuery query = N1qlQuery.parameterized("Select * from `quicktask` where species = 'fish/pira\u00f1a' ", JsonObject.create(), params);
System.out.println(quickProcessHistoryRepository.getCouchbaseOperations().getCouchbaseBucket().query(query));
I'm still trying to understand the behavior via SDK, I will update this answer as soon as I find the issue.
Documentation says it supports unicode.
https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/literals.html
Strings can be either Unicode characters or escaped characters.
Json strings can have unicode characters.
insert into default values ("f1",{"name":"fish/pira\u00f1a"});
select * from default where name = "fish/pira\u00f1a";
"results": [
{
"default": {
"name": "fish/piraña"
}
}
]
Collation (ORDER BY, indexing, ....) and data types comparison are based on byte comparison not based on unicode character. If unicode character is single/fixed byte it will work but if the data is variable multi-bytes may not work because comparison is based on byte comparison.

Parsing Infix Mathematical Expressions in Swift Using Regular Expressions

I would like to convert a string that is formatted as an infix mathematical to an array of tokens, using regular expressions. I'm very new to regular expressions, so forgive me if the answer to this question turns out to be too trivial
For example:
"31+2--3*43.8/1%(1*2)" -> ["31", "+", "2", "-", "-3", "*", "43.8", "/", "1", "%", "(", "*", "2", ")"]
I've already implemented a method that achieves this task, however, it consists of many lines of code and a few nested loops. I figured that when I define more operators/functions that may even consist of multiple characters, such as log or cos, it would be easier to edit a regex string rather than adding many more lines of code to my working function. Are regular expressions the right job for this, and if so, where am I going wrong? Or am I better off adding to my working parser?
I've already referred to the following SO posts:
How to split a string, but also keep the delimiters?
This one was very helpful, but I don't believe I'm using 'lookahead' correctly.
Validate mathematical expressions using regular expression?
The solution to the question above doesn't convert the string into an array of tokens. Rather, it checks to see if the given string is a valid mathematical expression.
My code is as follows:
func convertToInfixTokens(expression: String) -> [String]?
{
do
{
let pattern = "^(((?=[+-/*]))(-)?\\d+(\\.\\d+)?)*"
let regex = try NSRegularExpression(pattern: pattern)
let results = regex.matches(in: expression, range: NSRange(expression.startIndex..., in: expression))
return results.map
{
String(expression[Range($0.range, in: expression)!])
}
}
catch
{
return nil
}
}
When I do pass a valid infix expression to this function, it returns nil. Where am I going wrong with my regex string?
NOTE: I haven't even gotten to the point of trying to parse parentheses as individual tokens. I'm still figuring out why it won't work on this expression:
"-99+44+2+-3/3.2-6"
Any feedback is appreciated, thanks!
Your pattern does not work because it only matches text at the start of the string (see ^ anchor), then the (?=[+-/*]) positive lookahead requires the first char to be an operator from the specified set but the only operator that you consume is an optional -. So, when * tries to match the enclosed pattern sequence the second time with -99+44+2+-3/3.2-6, it sees +44 and -?\d fails to match it (as it does not know how to match + with -?).
Here is how your regex matches the string:
You may tokenize the expression using
let pattern = "(?<!\\d)-?\\d+(?:\\.\\d+)?|[-+*/%()]"
See the regex demo
Details
(?<!\d) - there should be no digit immediately to the left of the current position
-? - an optional -
\d+ - 1 or more digits
(?:\.\d+)? - an optional sequence of . and 1+ digits
| - or
\D - any char but a digit.
Output using your function:
Optional(["31", "+", "2", "-", "-3", "*", "43.8", "/", "1", "%", "(", "1", "*", "2", ")"])

String processing in Enterprise Architect CDT

Is it possible to somehow match and parse the string within transformation template (CDT) ?
I have a string: Dictionary and I would need:
1) detect tha string starts with Dictionary keyword
2)extract second parameter -> Shift
Sorted with following snippet - it is not most elegant way but works given the constraints
$dic=%FIND(opReturnType,"Dictionary")%
%If $dic!="-1"%
$note="TODO://Replace: with java.util.Map "
$note+=%opReturnType%
$t=%opReturnType%
$pos=%FIND($t,",")%
$dict=%LEFT($t,$pos)%
$ty=%REPLACE($t,$dict,"")%
$typ=%TRIM_LEFT($ty,",")%
$typp=%TRIM_RIGHT($typ,">")%
$type=%TRIM($typp)%
$lowerBound="0"
$upperBound="*"
%endIf%

RowFilter including [ character in search string

I fill a DataSet and allow the user to enter a search string. Instead of hitting the database again, I set the RowFilter to display the selected data. When the user enters a square bracket ( "[" ) I get an error "Error in Like Operator". I know there is a list of characters that need prefixed with "\" when they are used in a field name, but how do I prevent RowFilter from interpreting "[" as the beginning of a column name?
Note: I am using a dataset from SQL Server.
So, you are trying to filter using the LIKE clause, where you want the "[" or "]" characters to be interpreted as text to be searched ?
From Visual Studio help on the DataColumn.Expression Property :
"If a bracket is in the clause, the bracket characters should be escaped in brackets (for example [[] or []])."
So, you could use code like this :
DataTable dt = new DataTable("t1");
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Description", typeof(string));
dt.Rows.Add(new object[] { 1, "pie"});
dt.Rows.Add(new object[] { 2, "cake [mud]" });
string part = "[mud]";
part = part.Replace("[", "\x01");
part = part.Replace("]", "[]]");
part = part.Replace("\x01", "[[]");
string filter = "Description LIKE '*" + part + "*'";
DataView dv = new DataView(dt, filter, null, DataViewRowState.CurrentRows);
MessageBox.Show("Num Rows selected : " + dv.Count.ToString());
Note that a HACK is used. The character \x01 (which I'm assuming won't be in the "part" variable initially), is used to temporarily replace left brackets. After the right brackets are escaped, the temporary "\x01" characters are replaced with the required escape sequence for the left bracket.