json_extract_array_element_text does not unescape string values? - postgresql

Similarly to this question about json_extract_path_text, when I run this query in Redshift, I would expect json_extract_array_element_text to remove the backslashes from the "\"b\"" value:
select
j,
json_extract_array_element_text(j, 0) as a
from (select '["\\"b\\""]' as j);
Instead, it appears that the string value is extracted verbatim, and the results look like this:
["\"b\""]
\"b\"
Is this intentional? If yes, what would be the idiomatic way to remove the backslashes?

Related

How do I parse out a number from this returned XML string in python?

I have the following string:
{\"Id\":\"135\",\"Type\":0}
The number in the Id field will vary, but will always be an integer with no comma separator. I'm not sure how to get just that value from that string given that it's string data type and not real "XML". I was toying with the replace() function, but the special characters are making it more complex than it seems it needs to be.
is there a way to convert that to XML or something that I can reference the Id value directly?
Maybe use a regular expression, e.g.
import re
txt = "{\"Id\":\"135\",\"Type\":0}"
x = re.search('"Id":"([0-9]+)"', txt)
if x:
print(x.group(1))
gives
135
It is assumed here that the ids are numeric and consist of at least one digit.
Non-regex answer as you asked
\" is an escape sequence in python.
So if {\"Id\":\"135\",\"Type\":0} is a raw string and if you put it into a python variable like
a = '{\"Id\":\"135\",\"Type\":0}'
gives
>>> a
'{"Id":"135","Type":0}'
OR
If the above string is python string which has \" which is already escaped, then do a.replace("\\","") which will give you the string without \.
Now just load this string into a dict and access element Id like below.
import json
d = json.loads(a)
d['Id']
Output :
135

Check if string contains any of the following

I'm trying to check if a string contains one of four sub strings in a simpler way than this:
if (imageUrl.contains('.jpg') ||
imageUrl.contains('.png') ||
imageUrl.contains('.tif') ||
imageUrl.contains('.gif')) {
}
Is there a way to do this? For example checking against a list?
You can use a regex pattern instead of a simple string:
imageUrl.contains(new RegExp("\.(jpg|png|tif|gif)"))
Might be somewhat simpler.
RegularExpression can solve your problem. RegEx are used to search patterns in strings.
RegEx example:
^The matches any string that starts with The
end$ matches a string that ends with end
^The end$ exact string match (starts and ends with The end)
abc* matches a string that has ab followed by zero or more c

Using IN and Startswith operators together

I am trying to add a startswith operator to my formula below, because I need it to return all values starting TRA or MTA.
IF {STK_LOCATION.LOC_CODE}
IN ['TRA', 'MTA']
THEN {STK_LOCATION.LOC_STOCK_CODE}
ELSE {STK_LOCATION.LOC_STOCK_CODE} + LEFT({STK_LOCATION.LOC_CODE},4)
The IN function compares your input with the whole string. Try startswith:
IF({STK_LOCATION.LOC_CODE} startswith["TRA","MTA"]) THEN
{STK_LOCATION.LOC_STOCK_CODE}
ELSE
{STK_LOCATION.LOC_STOCK_CODE} + LEFT({STK_LOCATION.LOC_CODE},4)
And use always double quotes when you use strings, CR is not SQL

How to strip everything except digits from a string in Scala (quick one liners)

This is driving me nuts... there must be a way to strip out all non-digit characters (or perform other simple filtering) in a String.
Example: I want to turn a phone number ("+72 (93) 2342-7772" or "+1 310-777-2341") into a simple numeric String (not an Int), such as "729323427772" or "13107772341".
I tried "[\\d]+".r.findAllIn(phoneNumber) which returns an Iteratee and then I would have to recombine them into a String somehow... seems horribly wasteful.
I also came up with: phoneNumber.filter("0123456789".contains(_)) but that becomes tedious for other situations. For instance, removing all punctuation... I'm really after something that works with a regular expression so it has wider application than just filtering out digits.
Anyone have a fancy Scala one-liner for this that is more direct?
You can use filter, treating the string as a character sequence and testing the character with isDigit:
"+72 (93) 2342-7772".filter(_.isDigit) // res0: String = 729323427772
You can use replaceAll and Regex.
"+72 (93) 2342-7772".replaceAll("[^0-9]", "") // res1: String = 729323427772
Another approach, define the collection of valid characters, in this case
val d = '0' to '9'
and so for val a = "+72 (93) 2342-7772", filter on collection inclusion for instance with either of these,
for (c <- a if d.contains(c)) yield c
a.filter(d.contains)
a.collect{ case c if d.contains(c) => c }

How to make title interpret only part of a title string in Matlab

I'm creating a plot where I want to combine two strings into a title. I want to color the other part of my string. Here is my code (it will explain itself better):
title([csv_name ', {\color{blue}Bowel AUC: ' num2str(bowelAUC) ' }'])
In the variable csv_name I have a filename containing underscore _ characters and in the variable bowelAUC I have a number. I can color only part of my title string by using the guide in this post, that is using tex, but the problem now is that tex interpreter would also interpret the csv_name variable and I don't want this. Here you can see what I get:
The filename looks like this: ExportedPressure_130A_10-29-2014.csv
So I want title to interpret only the second part of my title, not the first...how to do this?
You need to replace _ by \_ so that TeX interprets them correctly. To do that you can use regexprep (note that within regexprep both characters are escaped again):
csv_name_escaped = regexprep(csv_name, '\_', '\\\_');
title([csv_name_escaped ', {\color{blue}Bowel AUC: ' num2str(bowelAUC) ' }'])