REGULAR EXPRESSIONS - nsregularexpression

I have a simple data frame:
test = { "City": ['A'], "Features":[ "Street20 - Statue of
liberty\n-Avenue40 - Manhattan\n-street40 - Opera320"]}
I am using regular expressions to pull out certain information to be shown as follows:
Land_marks
Statue of Liberty
Manhattan
Opera320
I am trying with regular expressions, but it only returns the first value "Statue of Liberty". Could you help me out?
Thank you in advance

You can use below regular expression #
(?<=\"Features\":\[\ ").*?(?=\])
Demo : https://regex101.com/r/HUVQoj/1
Why you are using regular expression to get data from JSON? You can convert JSON to object and fetch desire result.

Related

How to replace comma with a dot in GTM for JSON structured data?

I am noob with structured data implementation and don't have any code knowledge.
I have been looking for a week how to solve a warning with price in Google structured data testing tool.
My prices are with a comma which is not accepted by Google.
By checking the http://schema.org/price it tells me that "Use '.' (Unicode 'FULL STOP' (U+002E)) rather than ',' to indicate a decimal point. Avoid using these symbols as a readability separator."
I have a CSS variable element #PdtPrixRef named in a variable "Product-price" with a comma "12.5" but I can't find how to replace it in my structured data with the value "12.5"... Someone to help me?
Hereafter my actual script :
My actual GTM script
Should I add something to my script or making an VARIABLE (Custom Js)?
I think it's something like
value.replace(",", ".")
But I do't know how to write the full proper function from beginning to end...
Yes you can just create a Custom JavaScript Variable
Here is the code
function(){
var price = {{Product-price}};
return price.replace("," , ".");
}
Then using this variable to your JSON-LD script.

JSONPath - Filter on keys that contain string [duplicate]

This question already has answers here:
Find a JSON property name that starts with something using JSON Path
(2 answers)
Closed 1 year ago.
Is there a JSONPath expression to filter on keys that match a given pattern ?
I would like to get all the values that contain name in the keys without explicitly listing those keys.
Input
{"foo":{"bar":{"name1":"john","name2":"jane"}}}
Output (expected)
["john","jane"]
Thanks !
The vanilla approach to get the value of two keys is to use a $['x', 'y'] union.
In your case this could look like this:
$.foo.bar['name1', 'name2']
Unfortunately, not every JSON path engine will return the result in this way (and there are some minor syntax differences as well, i.e some prefer double quotes or no quotes, etc). E.g. when you test your input with the path above online here: https://jsonpath.herokuapp.com/
Using the Jayway tab yields:
{
"name1" : "john",
"name2" : "jane"
}
while the Gatling tab gives the expected result:
[
"john",
"jane"
]
So, give it a try with your JSON path engine at hand. If the result is not as expected post-processing the full or intermediate JSON result in a host programing language might be easier.
If you're having trouble with JsonPath because of the lack of standard syntax and specification, you can use JMESPath instead. It has implementations that pass the TCK in many languages.
foo.bar.[name1, name2]

Convert JSON nested data to dataframe in R

I have a json string that's is a nested dataframe and is full of characters that need to be escaped like \n, \r and \. I have not been able to convert it with jsonlite.
Here's a dput of the first element of the list.
fromJSON(json_data) gives the following error:
Replacing the character "{" with blank character is not working.
Any help would be greatly appreciated.
This solution is meant to be a stop-gap for one known flaw in the json validation: two (or more) dictionaries are not separated by a comma. I discourage the use of regular expressions to fix this, but a fixed string-replacement can suffice:
json_date <- gsub("} {", "},{", json_data, fixed = TRUE)

Pattern matching and replacing based from regex

I am trying to parse through a string and modify values that match a particular pattern. I am basically trying to convert the following R code into Python.
sample_formatted <- stringr::str_replace(sample,
'(\\b[a-zA-Z]+):([a-zA-Z]\\b)', '\\1\\2')
I am completely new to Python regex and a struggling to figure out where to start.
Any help will be greatly appreciated!
I guess then you can simply do a re.sub in Python:
import re
regex = r"(\b[a-zA-Z]+):([a-zA-Z]\b)"
test_str = ("abc:x\n"
"DEf:y\n"
"ABC:z")
subst = "\\1\\2"
result = re.sub(regex, subst, test_str)
if result:
print (result)
The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it.
RegEx Circuit
jex.im visualizes regular expressions:

R: Using regular expression to swap strings separated by a comma

So I have this list of names:
names <- c("stewart,pat", "peterson,greg")
from which I extract only the lastname,firstname items with the following regular expression:
myregexpr <- "(\\w+),(\\w+)?"
str_view(str_extract_all(names, myregexpr), myregexpr)
This yields a view like:
stewart,pat
peterson,greg
My question: Is there a way for me to write the regular expression such that the result would instead look like:
pat_stewart
greg_peterson
i.e. where the result of is first_last? I believe there is a way to do it as I've seen on other, similar questions. I've tried:
myregexpr <- "(\\w+),(\\w+)?\\2_\\1"
but that returns only `character(0)'. I've attempted many versions - some of which crash R studio. Any ideas?

Resources