So the JSON is like:
"foo": {
"points": 23.67
},
I'd like a regex to just match 23.67.
I've tried \"foo\":{\"points\":([^}"]*) but it doesn't work.
There are multiple lines which contain "points": so just \"points\":([^}"]*) won't work.
You are ignoring whitespace.
Try this instead:
\"foo\":\s*{\s*\"points\":\s*(\d+(?:\.\d+)?)\s*}
Demo
Your solution does not take into account a few details:
Between "foo": and { there can be spaces.
After { there can be a newline and spaces.
After "points": there can also be spaces.
Between the string to capture (capturing group and "terminating" '}'
there can also be a newline and spaces.
So, including the above missed details, and taking into account that \s
matches also newline, the whole regex can be as follows:
\"foo\":\s*{\s*\"points\":\s*([^}"]*)\s*}
Actually, your capturing group can be "more restrictive".
As the text to capture contains only digits and a dot,
it can be written as: [\d\.]+.
Note that I changed * to +, because the content cannot be empty.
Related
I have a JSON text full of these:
"order": "Commande",
"#order": }
"description": "order word",
"type": "texte"
},
As you can see, There is an error in front of "#Order:" which } is used instead of {
How can I replace all of them with open bracket without changing }, at the end of objects? (I mean I need the regex expression to use it in search of my text editor)
^\s\}{1}\s didn't work
Maybe this can help you
(?<=\"#\w+\":\s+)}
https://regex101.com/r/A4Yv6X/1
Your ^\s\}\s pattern matches a whitespace, } and whitespace at the start of string. while the } you want to replace is at the end of string (or line).
You may consider using \}$, or \}(?=\s*$), or \}(?=\h*$) patterns to match } only at the end of a string/line (where $ is the end of string/line, (?=\s*$) matches a location that is immediately followed with any 0 or more whitespaces and then the end of a string/line, \h just only allows horizontal whitespaces).
However, if there is a colon before the } you need to replace you may consider a more sophisticated pattern like
(:\h*)\}(\h*$)
(:[^\S\r\n]*)\}([^\S\r\n]*$)
Replace with $1{$2 (or \1{\2 depending on the environment).
See the regex demo. Details:
(:\h*) - Capturing group 1 ($1): a colon and zero or more horizontal whitespaces
\} - a } char
(\h*$) - Group 2 ($2): zero or more horizontal whitespaces.
Note that [^\S\r\n] is a rough equivalent to \h, it matches any whitespace char but CR and LF chars.
Try this, which matches brackets at the ends of lines
(?m)\}$
See live demo.
Depending on your input, this might be enough.
I am doing a select from a table and I want the output to be in json. For this I am using the json_build_object function.
I am getting some unexpected behavior when the select has a value with a slash inside of it.
A simple example would be:
select json_build_object('test', 'a\b');
This outputs:
{ "test": "a\\b" }
I would like to get an output of:
{ "test": "a\b" } // without extra backslash
Since your input is three characters including the first two letters and a literal backslash between them, then "a\\b" is the correct way to represent that in JSON.
If your input were made up of 2 characters, the first letter and the 'backspace' control character, that would correctly be represented in JSON as "a\b". But that is not the input you showed us. If your input is not what you want it to be, you shouldn't expect json_build_object to read your mind and fix it for you.
I have the following JSON file. Dotted across the file is the following:
"properties": {
"Name": "Darlington",
"Description": "<br><br><br> <table border=\"1\" padding=\"0\"> <tr><td>CCGcode</td><td>00C</td></tr> <tr><td>CCGname_short</td><td>Darlington</td></tr>"
}
Using RegEx, I would like to extract the CCG Code property and add it back in so that the above becomes:
"properties": {
"Name": "Darlington",
"CCGcode": "00C",
"Description": "<br><br><br> <table border=\"1\" padding=\"0\"> <tr><td>CCGcode</td><td>00C</td></tr> <tr><td>CCGname_short</td><td>Darlington</td></tr>"
}
I've tried all sorts and I just can't get it to work. I am using Sublime Text.
^("Description":").*?<td>CCGcode<\/td><td>([^<>\n]*).*$
The above selects the code, but not sure how I can get it to create the property.
Try this
( *)"Description".*?CCGcode.*?<td>([^<]+)
Regex demo
This one for sublimetext3
Find what:
( *)("Description".*?CCGcode.*?<td>)([^<]+)
Replace with:
\1"CCGcode": "\3",\n\1\2
Demo
There's a very simpel, but not so elegant, solution. Replace
"Description":.*?<td>CCGcode<\/td><td>([A-Z\d]*)<\/td>
with
"CCGCode":"\1",\n \0
Don't know how Sublime handles replacements, but you may have to change the replacing \0 and \1 to something else - e.g. $0 and $1.
What it does is to find the Description entry and the following CCGCode entry, capturing the code into capture group 1.
Then replace capture group 0 - the whole matched text, with the new CCGCode JSON tag plus the original text.
It's a pretty fragile solution, but it works for your sample case.
Check out example at regex101.
Regards
Why do I get 0 when running this expression?
SELECT 'Nr. 1700-902-8423. asdasdasd' REGEXP '1+[ ,.-/\]*7+[ ,.-/\]*0+[ ,.-/\]*0+[ ,.-/\]*9+[ ,.-/\]*0+[ ,.-/\]*2+[ ,.-/\]*8+[ ,.-/\]*4+[ ,.-/\]*2+[ ,.-/\]*3+';
I need to get true, when the text contains the specified number (17009028423). There can be symbols ,.-/\ between digits.
For example, if I have number 17009028423, I need get true when in text is:
1700-902-8423
17-00,902-84.23
170/09-0.28\423
1700..902 842-3
17,.009028 4//2\3
etc.
Thanks.
There are two problems with your regular expression. First is that backslash in \] escapes the special meaning of ] to denote a character class. You need to escape your backslash: \\]. Another problem is that - denotes a range [ and ] (e.g. [a-zA-Z]). You need to escape that too or put it at the end like [a-zA-Z-] (as #tenub said). Plus the backslashes should be escaped themselves, which makes:
SELECT 'Nr. 1700-902-8423. asdasdasd' REGEXP '1[ ,./\\\\-]*7[ ,./\\\\-]*0[ ,./\\\\-]*0[ ,./\\\\-]*9[ ,./\\\\-]*0[ ,./\\\\-]*2[ ,./\\\\-]*8[ ,./\\\\-]*4[ ,./\\\\-]*2[ ,./\\\\-]*3'
You can check for yourself.
I also removed + signs in case you want to match each number only once.
I am trying to make my regex work across multiple lines and "m" didn't seem to work either. So, my regex is working for 1st line and noT for the following lines.
You can skip the match part and just do it all in one step:
> "the *text* is to be replaced \n by *text*".replace(/\*([\s\S]*?)\*/g, '<i>$1</i>');
"the <i>text</i> is to be replaced \n by <i>text</i>"
. matches any character, but it excludes newlines. [\s\S] matches any character including newlines.
I changed your search regex to \*([\s\S]*?)\*, which non-greedily matches the stuff between the asterisks.
The replacement string is <i>$1</i>. $1 is replaced with the contents of the first capturing group, which is your text.
Also, because it looks like you're trying to convert Markdown to HTML, try using a pre-made JS converter: http://www.showdown.im/
You can use it like this:
var str = "the *text* is to be *replaced \n by* *text*";
alert(str.replace(/\*([\s\S]*?)\*/g, '<i>$1</i>'));