Regex expression for Changing close brackets to open brackets - json

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.

Related

Postgres json_build_object adding backslash to text column

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.

How to match within two lines in JSON using REGEX?

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.

Sublime Text regex to find and replace whitespace between two xml or html tags?

I'm using Sublime Text and I need to come up with a regex that will find the whitespaces between a certain opening and closing tag and replace them with commas.
Example: Replace white space in
<tags>This is an example</tags>
so it becomes
<tags>This,is,an,example</tags>
Thanks!
You have just to use a simple regex like:
\s+
And replace it with with a comma.
Working demo
This will find instances of
<tags>...</tags>
with whitespace between the tags
(<tags>\S+)\W(.+</tags>)
This will replace the first whitespace with a comma
\1,\2
Open Find and Replace [OS X Cmd+Opt+F :: Windows Ctrl+H]
Use the two values above to find and replace and use the 'Replace All' option. Repeat until all the whitespaces are converted to commas.
The best answer is probably a quick script but this will get you there fairly fast without needing to do any coding.
You can replace any one or more whitespace chunks in between two tags using a single regular expression:
(?s)(?:\G(?!\A)|<tags>(?=.*?</tags>))(?:(?!</?tags>).)*?\K\s+
See the regex demo. Details
(?s) - a DOTALL inline modifier, makes . match line breaks
(?:\G(?!\A)|<tags>(?=.*?</tags>)) - either the end of the previous successful match (\G(?!\A)) or (|) <tags> substring that is immediately followed with any zero or more chars, as few as possible and then </tags> (see (?=.*?</tags>))
(?:(?!</?tags>).)*? - any char that does not start a <tags> or </tags> substrings, zero or more occurrences but as few as possible
\K - match reset operator
\s+ - one or more whitespaces (NOTE: use \s if each whitespace must be replaced).
SublimeText settings:

Regex issue on SQL

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.

Regex all uppercase with special characters

I have a regex '^[A0-Z9]+$' that works until it reaches strings with 'special' characters like a period or dash.
List:
UPPER
lower
UPPER lower
lower UPPER
TEST
test
UPPER2.2-1
UPPER2
Gives:
UPPER
TEST
UPPER2
How do I get the regex to ignore non-alphanumeric characters also so it includes UPPER2.2-1 also?
I have a link here to show it 'real-time': http://www.rubular.com/r/ev23M7G1O3
This is for MySQL REGEX
EDIT: I didn't specify I wanted all non-alphanumeric characters (including spaces), but with the help of others here it led me to this: '^[A-Z-0-9[:punct:][:space:]]+$' is there anything wrong with this?
Try
'^[A-Z0-9.-]+$'
You just need to add the special characters to the group, optionally escaping them.
Additionally if you choose not to escape the -, be aware that it should be placed at the start or the end of the grouping expression to avoid the chance that it may be interpreted as delimiting a range.
To your updated question, if you want all non-whitespace, try using a group such as:
^[^ ]+$
which will match everything except for a space.
If instead what you wanted is all non-whitespace and non-lowercase, you likely will want to use:
^[^ a-z]+$
The 'trick' used here is adding a caret symbol after the opening [ in the group expression. This indicates that we want the negation of the match.
Following the pattern, we can also apply this 'trick' to get everything but lowercase letters like this:
^[^a-z]+$
I'm not really sure which of the 3 above you want, but if nothing else, this ought to serve as a good example of what you can do with character classes.
I believe you are looking for (one?) uppercase-word match, where word is pretty much anything.
^[^a-z\s]+$
...or if you want to allow more words with spaces, then probably just
^[^a-z]+$
You just need to put in the . and -. In theory, you don't need to escape because they are inside the brackets, but I like to to remind myself to escape when I have to.
'^[A-Z0-9\.\-]+$'
Try regular expression as below:
'^[A0-Z0\\.\\-]+$'