apache camel: replace function - replace

I want to use replace function in apache camel but have trouble with this. here is my code:
"firstName" : "aaa",
"lastName" : []
and my desire output is like this:
"firstName" : "aaa",
"lastName" : ""
I use this code for transform:
<transform>
<simple>${body.replace("[]","")}</simple>
</transform>
but the problem is not showing double quotation, actually I get this:
"firstName" : "aaa",
"lastName" :
I used &quot also, but do not work. Thanks in advance

I guess the correct replace call would contain escaped double quotes (I use Java DSL)
.transform(simple("${body.replace('[]', '\"\"')}"))
But that does not work, the empty double quotes do not show up in the result. Perhaps they are "evaluated to empty".
Original body: "firstName" : "aaa" "lastName" : []
Transformed body: "firstName" : "aaa" "lastName" :
However, you can do a workaround by replacing empty brackets in a first step to empty double quotes with a "special helper character" just to make sure the replace string cannot be interpreted as empty. I used the plus sign (+) in my example below.
In a second step you can replace the helper character with nothing and so the empty double quotes stay in the result.
.transform(simple("${body.replace('[]', '+\"\"')}"))
.transform(simple("${body.replace('+', '')}"))
This works for me.
Original body: "firstName" : "aaa" "lastName" : []
Transformed body: "firstName" : "aaa" "lastName" : ""
You would need to use a helper character that is not contained in your body. Otherwise the second step would perhaps remove something you do not want to.

Related

How to detect text inside specific character on Dart

I am trying to create a function to detect if some text is encapsuled by a special symbol/character , so if i have String :
String text = 'This is normal String, <b> This is Bold String <b>';
i would get a List of Map like this :
[ {'This is Normal String' : 'normal'} ,
{'This is Bold String' : 'bold'} ]
Then i can rewrite it on RichText,
What i have tried is splitting the text like this: List<String> list = text.split('<b>');
and make the even index of the list bold, but it will not behave the way i wanted if the bold tag is on the front of the text , and if i need to detect another character like <i> , is there any way to do this ?
Thankyou
var string = 'I am here';
string.contains('h');// true
//You can use Regex to find patterns inside a string
string.contains(new RegExp(r'[A-Z]')); // true

How json treats if there is a forward slashes in json string

I Have a json string as below:
[
{
"NAME" : "KANTESH",
"SNAME" : "NAGARADDER",
"MSG1" : "\"HELLO:HOW ARE YOU\"\n",
"MSG2" : "\"HELLO:///HOW ARE YOU\"\n",
"ID" : 20074499
}
]
when i am trying to remove the white spaces using libjson libarary function i.e json_strip_white_space() i am getting the below resulting string.
[{"NAME":"KANTESH","SNAME":"NAGARADDER","MSG1":"\"HELLO:HOW ARE YOU\"\n","MSG2":"\"HELLO:"ID":20074499}]
In the above resulting string the characters after the '///' are stripped out and concatenated with next line.
Below are my queries:
1)Why the characters after the forward slashes are stripping out.
2)Does json_strip_white_space() treats '///' as comments
3)How to overcome this problem
Please help me.
Thanks,

Regex to replace all occurences of a character within a given context

I am trying to write a search and replace regex (in ruby) to replace all instances of a character in a string in a given context.
The regex needs to replace all instances of "." in a json key, and I'm battling with references. I have a feeling that I need to use a lookaround in some way, but the variations I've tried I can't seem to get working.
Some example strings:
, "key1.name" : " value.something "
, "key2.complex.name" : "value.else"
, "this.is.the.most.complex.name" : "value"
I initially had this regex to replace a single occurrence (replacing it with "FULLSTOP"):
s/, "([^.]+)\.([^"]+)" :/, "\1FULLSTOP\2" :/gā€ā€
Desired output:
, "key1FULLSTOPname" : " value.something "
, "key2FULLSTOPcomplexFULLSTOPname" : "value.else"
, "thisFULLSTOPisFULLSTOPtheFULLSTOPmostFULLSTOPcomplexFULLSTOPname" : "value"
I'm guessing I need to use a (?=\.) somehow in the search, but not sure how to use this correctly with references. I am using the opening , and ending : as a way of defining the context for a json key.
thanks in advance.
(?=.*?\:)\.
Use this.See demo.
http://regex101.com/r/cH8vN2/5
Edit:
(?=.*?\"\s*\:)\.
Use this to be very sure.
See demo.
http://regex101.com/r/cH8vN2/6
You can use the following as a sample :
str = ', "this.is.the.most.complex.name" : "value';
str = str.gsub(/\.+/, 'FULLSTOP');
puts str;
I have not taken care of the 'value' part.
You should be able to do that easily.

MongoDB querying whitespace with regex

I've got a large collection of text data stored in MondoDB that users can query via keyword or phrase, and have an issue where some data has unicode character U+00A0 (no-break space) instead of a regular space.
Fixing up the data not being an option (those nbsps are there intentionally), I still want the user to be able to search and find that data. So I updated our Mongo query-building code to search for any whitespace [\s] in places where the user entered a space, resulting in a query like so:
{ "tt" : { "$elemMatch" : { "x" : { "$regex" : "high[\s]performance" , "$options" : "i"} }}}
(there's more to the query, that's just the relevant bit).
Unfortunately, this doesn't return the expected results. So I play around with a bunch of other ways to accomplish this, and eventually discover that I get the correct results when I search for "not non-whitespace" [^\S], as so:
{ "tt" : { "$elemMatch" : { "x" : { "$regex" : "high[^\S]performance" , "$options" : "i"} }}}
Which leads to my question -- why does "any whitespace" ("\s") fail finding this text while "not-non whitespace" ("^\S") finds it successfully? Does Mongo have a different set of rules for what counts as whitespace and non-whitespace?
Data is all in UTF-8 throughout, MongoDB version is 2.2.2
I suppose that the problem here is with \, not with spaces. Can you please write \\ to prove my conjecture?

Replace using regex containing some of previous match in Notepad++

I'm trying to use a regex to match a block of text, and using replace all, replace it with nothing, so as to delete it.
But Since I sometimes (but not always) have the block appear one after another when I try to replace all, it replaces every second block.
I made this Regex
http.*\n.*\K\n\{\n "code"(.*\n)+?\}\nhttp.*\n
But it will match all isolated blocks, but only every second consecutive block.
I think I'm meant to use "assertions" as described by here. But I couldn't get them to work.
Also how do I replace with nothing (as in delete)? Just leave an empty replace with field? or do I need some special character? Or as I am coming to suspect, I shouldn't use Notpad++ for this sort of thing? If that is the case what should/could I be using?
Sample Data:
"teamAbbr" : "Foo",
"teamName" : "Bar",
"teamNickname" : "FBar"
}
} ]
}
http://www.link_I_want_to_keep_belonging_to_above_data.com
{
"code" : "XXXXXXXXXXXXXXXXXXXXXXX",
"techMessage" : "XXXXXXXXXXXXXXXXXXXXXX",
"userMessage" : "XXXXXXXXXXXXXXXXXXX",
"host" : "XXXXXXXXXXXX",
"date" : "XXXXXXXXXXX",
"version" : "XXX"
}
http://www.url_that_belong_to_block_Iwant_to_be_rid_off.com
{
"code" : "XXXXXXXXXXXXXXXXXXXXXXX",
"techMessage" : "XXXXXXXXXXXXXXXXXXXXXX",
"userMessage" : "XXXXXXXXXXXXXXXXXXX",
"host" : "XXXXXXXXXXXX",
"date" : "XXXXXXXXXXX",
"version" : "XXX"
}
http://www.url_that_belong_to_block_Iwant_to_be_rid_off.com
The problem is that you also match the first url, but that is unavailable when immidiately after a match. And also at the start of the file.
Lookbehind assertions takes care of the problem, but needs to be fixed length.
Do you need to search for the first url? Ie. does
\{\n "code"(.*\n)+?\}\nhttp.*\n
work for you?
To delete a whole match you replace with an empty string. No special characters needed.