Netezza, Is there a way to replace a character by multiple characters? - replace

I need to replace a character by multiple characters, i tried using Translate, but it just replace one by one according to the sequence defined in my case i need something like this
function( axbxc, "x", "yz") = ayzbyzc
if I use
translate( axbxc, "x", "yz") i get aybyc
Is there any way to achieve this ?
Thanks
Camilo

What are you looking for is replace function
select replace( 'axbxc', 'x', 'yz')

Related

Need some guidance in regular expressions substring

Three string examples: (data is stored in a column called a.metadata)
anything_before"subjectareas":[{"name":"\"I NEED THIS1\""anything_after
anything_before"subjectareas":[{"displayName":"whatever","name":"\"I NEED THIS2\""anything_after
anything_before"subjectareas":[{"displayName":"whatever - whatever","name":"\"I NEED THIS3\""anything_after
My output should be:
I NEED THIS1
I NEED THIS2
I NEED THIS3
The words subjectareas, name, displayName can be used in the logic and case sensitive based expressions are OK.
I was trying a variety of things, including this below, but I can't get it to work.
regexp_substr(a.metadata, '[^"subjectareas"]*[^"name":"\"]*[^\\]')
Any help is appreciated.
I got it to work using a nested regexp_replace and two regexp_substr functions. It is ugly. There has to be a better way to do this:
regexp_replace(regexp_substr(regexp_substr(a.metadata, 'subjectareas[^\\]\\[^\\]', 1, 1, 'c'), '\\.*'),'\\|"')
Use
SELECT REGEXP_SUBSTR(a.metadata,
'"subjectareas":\[\{("[^"]*":"[^"]*",)*"name":"\W*"([^\\]*)',
1, 1, 'c', 2)
See regex proof. You get the second group value with the above REGEXP_SUBSTR.

Regex to select text outside of underscores

I am looking for a regex to select the text which falls outside of underscore characters.
Sample text:
PartIWant_partINeedIgnored_morePartsINeedIgnored_PartIwant
Basically I need to be able to select the first keyword which is always before the first underscore and the last keyword which is always after the last underscore. As an additional complexity, there case also be texts which have no underscore at all, these need to be selected completely as well.
The best I got yet was this expression:
^((?! *\_[^)]*\_ *).)*
which is only yielding me the first part, not the second and it has no support for the non-underscore yet at all.
This regex is used in a tool which monitors our http traffic, which means I can only 'select' the part I need but can't invoke functions or replace logic.
Thanks!
Use JavaScript string function split(). Check below example.
var t = "PartIWant_partINeedIgnored_morePartsINeedIgnored_PartIwant";
var arr = t.split('_');
console.log(arr);
//Access the required parts like this
console.log(arr[0] + ' ' + arr[arr.length - 1]);
Perhaps something like this:
/(^[^_]+)|([^_]+$)/g
That is, match either:
^[^_]+ the beginning of the string followed by non-underscores, or
[^_]+$ non-underscores followed by the end of the string.
var regex = /(^[^_]+)|([^_]+$)/g
console.log("A_b_c_D".match(regex)) // ["A", "D"]
console.log("A_b_D".match(regex)) // ["A", "D"]
console.log("A_D".match(regex)) // ["A", "D"]
console.log("AD".match(regex)) // ["AD"]
I'm not sure if you should use a regex here. I think splitting the string at underscore, and using the first and last element of the resulting array might be faster, and less complicated.
Trivial with .replace:
str.replace(/_.*_/, '')
// "PartIWantPartIwant"
With matching, you'd need to be selecting and concatenating groups:
parts = str.match(/^([^_]*).*?([^_]*)$/)
parts[1] + parts[2]
// "PartIWantPartIwant"
EDIT
This regex is used in a tool which monitors our http traffic, which means I can only 'select' the part I need but can't invoke functions or replace logic.
This is not possible: a regular expression cannot match a discontinuous span.

Why does Coldfusion strip out zeros in list function

I've run into a strange behavior with Coldfusion 10- I have a variable named myString, "12600A07xxx".
I want the substrings before and after "A07", so I use "A07" as a list delimiter, and type listFirst(myString, "A07") and listLast(myString, "A07").
For the first, it's stripping out the zeros, and returning "126" when it should be returning "12600". It does the same with ListGetAt(). Why is this happening?
I see - CF is treating "A07" as three different delimiters, "A", "0", and "7", instead of a single delimiter, "A07". And there a couple of ways to deal with this, the simplest being to temporarily substitute "A07" with a single character such as a pipe and use that as the temporary delimiter.
As you already noticed, ColdFusion has many functions with single character delimiters. However, there are also functions that support multi-character delimiters, e.g.
listToArray( list [, delimiters[, includeEmptyFields[, multiCharacterDelimiter]]] ).
Example:
parts = listToArray("12600A07xxx", "A07", false, true);
writeOutput( parts[1] ); // >> 12600
writeOutput( parts[2] ); // >> xxx
On a side note: Are you sure you want to split the input using a specific delimiter? You might be better off using regular expression patterns with this kind of data. Just a guess though.
Another approach could be to use reReplaceNoCase(). Like this:
theString = "12600A07xxx";
prefix = reReplaceNoCase(theString , "A07.*", "");
suffix = reReplaceNoCase(theString , ".*A07", "");

Need to parse the string separated by colon and eliminate part of the string if found the match in plsql

I know this topic was discussed multiple times, I looked at multiple posts and answers, but could not find exactly what I need to do.
I am trying to search the string, that has multiple values of varchar2 separated by ':', and if the match is found on another string, delete part of the string that matched, and update the table with the rest of the string.
I wrote the code using combination of str and instr functions, but looking for more elegant solution using regexp, or collections.
For example, the input string looks like this: ('abc:defg:klmnp). Need to find for example the piece of the string (could be at any position), and remove it, that result would look like this: (abc:klmnp)?
EDIT - copied from comment:
The input string (abc:defg:klmn:defgb). Let's say I am looking for defg, and only defg will have to be removed, not defgb. Now, like I mentioned before, next time around, I might be looking for the value in position 1, or the last position. So the desired part of the string to be removed might not always be wrapped in ':' from the both sides, but depending where it is in the string, either from the right, or from the left, or from both sides.
You can do this with a combination of LIKE, REPLACE and TRIM functions.
select trim(':' from
replace(':'||your_column||':',':'||search_string||':',':')
) from table_name
where ':'||your_column||':' like '%:'||search_string||':%';
Idea is,
Surround the column with colons and use LIKE function to find the match.
And on such matched rows, use REPLACE to replace the search string along with surrounding colons, with a single colon.
And then use TRIM to remove the surrounding colons.
Demo at sqlfiddle
EDIT (simplified) Perhaps this is what you need:
SELECT REGEXP_REPLACE(REPLACE('abc:defg:klmnop', ':defg:', ':'), '(^defg:|:defg$)', '')
, REGEXP_REPLACE(REPLACE('defg:klmnop:abc', ':defg:', ':'), '(^defg:|:defg$)', '')
, REGEXP_REPLACE(REPLACE('abc:klmnop:defg', ':defg:', ':'), '(^defg:|:defg$)', '')
, REGEXP_REPLACE(REPLACE('abc:klmnop:defgb:defg', ':defg:', ':'), '(^defg:|:defg$)', '')
FROM DUAL
;
which removes defg from start, middle, and end, and ignores defgb, giving:
abc:klmnop
klmnop:abc
abc:klmnop
abc:klmnop:defgb
And to update the table, you could:
UPDATE my_table
SET value = REGEXP_REPLACE(REGEXP_REPLACE(value, ':defg:', ':'), '(^defg:|:defg$)', '')
-- WHERE REGEXP_LIKE(value, '(^|.*:)defg(:.*|$)')
WHERE value LIKE '%defg%'
;
(though that final regex for the where may need to be tweaked to match, hard to test...)

Selectively deleting commas and splitting strings

I have s string that looks like
txt = '"EMB","iShares J,P. Morg",110.81,N/A'
I'm using strsplit(txt,','); to break this into separate strings based on the comma delimiter. However I want to ignore the comma between the 'J' and the 'P', since it's not a delimiter; it's just part of the name.
Is there a way I can say "if a comma is between two quotes but there are other characters between the quotes, delete the comma"?
Here's an equivalent regexp one-liner:
C = regexp(txt, '("[^"]*")|([^,"]+)', 'match')
The result is a cell array with already split strings. Unfortunately, I don't have MATLAB R2013, so I cannot benchmark this versus strsplit.
A silly (but functional) answer:
inquotes=false;
keep=true(1,length(txt));
for v=1:length(txt)
if (txt(v)=='"')
inquotes=~inquotes;
elseif (txt(v)==',' && inquotes)
keep(v)=false;
end
end
txt=txt(keep);
tt=strsplit(txt,',');
This will, if you are in quotes, remove the commas so that you can use strsplit. That is what I understood you want to do, correct?