Visual Studio Code regex for (not (xx)) - regex

I am trying to have a regex to capture all strings with this shape:
(not (xx))
I.e. a parenthesis, a "not", two empty spaces, two caracters between parenthesis and a closing parenthesis
I have tried:
(not (*))
But I get:
Invalid regular expression. Nothing to repeat
Any idea ?

The Nothing to repeat error is due to the * quantifier used to quantify an opening bracket of a capturing group construct.
You need to 1) escape special ( and ) chars, and 2) match any text between the closest ( and ) with a negated character class, here, with [^()]*:
\(not \([^()]*\)\)
Details:
\(not \( - a (not ( string
[^()]* - zero or more chars other than ( and )
\)\) - a )) string.
If there can be any zero or more whitespace chars between not and (, replace the literal spaces with \s*. If there must be one or more whitespaces, use \s+ instead:
\(not\s*\([^()]*\)\)
\(not\s+\([^()]*\)\)

Related

Exclude curly brace matches

I have the following strings:
logger.debug('123', 123)
logger.debug(`123`,123)
logger.debug('1bc','test')
logger.debug('1bc', `test`)
logger.debug('1bc', test)
logger.debug('1bc', {})
logger.debug('1bc',{})
logger.debug('1bc',{test})
logger.debug('1bc',{ test })
logger.debug('1bc',{ test})
logger.debug('1bc',{test })
Instead of debug there can be other calls like warn, fatal etc.
All quote pairs can be "", '' or ``.
I need to create a regular express which matches case 1 - 5 but not 6 - 11.
That's what I've come up with:
logger.*\(['`].*['`],\s*.([^{.*}])
This also matches 8 - 11, so I'm suspecting this part is wrong ([^{.*}]) but I don't get it why.
You can try this
logger\.[^(]+\((?:"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|`(?:\\`|[^`])*`),[^{}]*?\)
Regex Demo
P.S:- This pattern can be shorten if we are sure there won't be any mismatch of quotes, also if there won't be any escaped quote inside string
If there's no escaped string
logger\.[^(]+\((?:"[^"]*"|'[^']*'|`[^`]*`),[^{}]*?\)
If there's no quotes in between string. i.e no strings like "mr's jhon
logger\.[^(]+\(([`"'])[^"'`]*\1,[^{}]*?\)
If there are no quotes between the quoted parts, you could make use of a capturing group to match one of the quote types (['`"]) and use a backreference \1 to match the closing quote type.
The \r\n in the negated character class is to not cross newline boundaries.
The pattern will match either the quoted parts or 1+ times a word character for the first part.
The second part matches any char except { or } or ) using a negated character class.
logger\.[^(\r\n]+\((?:(['`"])[^'`"]+\1|\w+),[^{})\r\n]+\)
That will match
logger\. Match logger.
[^(\r\n]+ Match 1+ times any char except ( or a newline
\( Match (
(?: Non capture group
(['`"]) Capture group 1
[^'`"]+\1 Match 1+ times any char except the quote types, backreference to the captured
| or
\w+ Match 1+ word chars
), Close non capture group and match ,
[^{})\r\n]+ Match 1+ times any char except { } ) or a newline
\) Match )
Regex demo

Why ex (vim) is catching matching pattern to the end of line

File contains following line:
[assembly: AssemblyVersion("1.0.0.0")]
Bash script that replaces one version to another:
echo "%s/AssemblyVersion\s*\(.*\)/AssemblyVersion(\"$newVersionNumber\")]/g
w
q
" | ex $filePath
The question is why this catch whole line to the end so i have to add ] at the end of replacement string?
The problem arises because .* matches all the chars to the end of the line, and \( and \) create a capturing group (unlike most of NFA regex engines, Vim regex matches a ( char with an unescaped ( and ) with an unescaped ) in the pattern).
You may use
%s/AssemblyVersion\s*([^()]*)/AssemblyVersion(\"$newVersionNumber\")/g
Here, AssemblyVersion will match the word, then \s* will match any 0+ whitespace chars, ( will match a literal (, [^()]* will match 0+ chars other than ( and ), and ) will match a literal ).
Another regex substitution command you may use is
:%s/AssemblyVersion\s*(\zs[^()]*\ze)/\"$newVersionNumber\"/g
Here, AssemblyVersion\s*( will match AssemblyVersion, 0+ whitespaces and ( and \zs will omit that part from the match, then 0+ chars other than ( and ) will get matched, and then \ze) will check if there is ) to the right of the current location, but won't add it to the match.
\zs sets the next character to be the first character of the match. Any text before the \zs pattern will not be included into the match.
\ze sets the end of the match. Anything after the \zs pattern will not be part of the match.

notepad++ remove text between two string using regular expression

I want to remove text between two strings using regular expression in notepad++. Here is my full string
[insertedOn]) VALUES (1, N'1F9ACCD2-3B60-49CF-830B-42B4C99F6072',
I want final string like this
[insertedOn]) VALUES (N'1F9ACCD2-3B60-49CF-830B-42B4C99F6072',
Here I removed 1, from string. 1,2,3 is in incremental order.
I tried lot of expression but not worked. Here is one of them (VALUES ()(?s)(.*)(, N')
How can I remove this?
You may use
(VALUES \().*?,\s*(N')
and replace with $1$2. Note that in case the part of string to be removed can contain line breaks, enable the . matches newline. If the N and VALUES must be matched only when in ALLCAPS, make sure the Match case option is checked.
Pattern details
(VALUES \() - Group 1 (later referred with $1 from the replacement pattern): a literal substring VALUES (
.*? - any 0+ chars, as few as possible, up to the leftmost occurrence of the sunsequent subpatterns
,\s* - a comma and 0+ whitespaces (use \h instead of \s to only match horizontal whitespace chars)
(N') - Group 2 (later referred with $2 from the replacement pattern): a literal substring N'.
You should first escape literal ( before VALUES: \(
By doing so, .* in your regex in addition to s (DOTALL) flag causes engine to greedily match up to end of input string then backtracks to stop at the first occurrence of , N' which means unexpected matches.
To improve your regex you should 1) make .* ungreedy 2) remove (?s) 3) escape (:
(VALUES \().*?, (N')
To be more precise in matching you'd better search for:
VALUES \(\K\d+, *(?=N')
and replace with nothing.
Breakdown:
VALUES \( March VALUES ( literally
\K Reset match
\d+, * Match digits preceding a comma and optional spaces
(?=N') Followed by N'

Regular expression to replace content parentheses and their contents

I am looking for a regular expression that will replace parentheses and the strings within them if the string anything that is not a digit.
The string can be any combination of characters including numbers, letters, spaces etc.
For example:
(3) will not be replaced
(1234) will not be replaced
(some letters) will be replaced
(some letters, spaces - and numbers 123) will be replaced
So far I have a regex that will replace any parentheses and its content
str = str.replaceAll("\\(.*?\\)","");
I am not good with the syntax of replaceAll, so I am just going to write the way you have written it. But I think I can help you with the regex.
Try this Regex:
\((?=[^)]*[a-zA-Z ])[^)]+?\)
Demo
OR an even better one:
\((?!\d+\))[^)]+?\)
Demo
Explanation(for 1st Regex)
\( - matches opening paranthesis
(?=[^)]*[a-zA-Z ]) - Positive Lookahead - checks for 0 or more of any characters which are not ) followed by a space or a letter
[^)]+? - Captures 1 or more characters which are not )
\) - Finally matches the closing Paranthesis
Explanation(for 2nd Regex)
\( - matches opening paranthesis
(?!\d+\)) - Negative Lookahead - matches only those strings which do not have ALL the characters as digits after the opening paranthesis but before the closing paranthesis appears
[^)]+? - Captures 1 or more characters which are not )
\) - Finally matches the closing Paranthesis
Now, you can try your Replace statement as:
str = str.replaceAll("\((?=[^)]*[a-zA-Z ])[^)]+?\)","");
OR
str = str.replaceAll("\((?!\d+\))[^)]+?\)","");

Regular Expression to parse some special cases of C Code

I am trying to check generated C Code with a regular expression.
Actually the lines I need to check always start the same way
R_Wrt_somename(V_var)
or
R_Wrt_othername((int64) (V_var2 * 3))
I already got an expression for the first one, but I am not able to get a fitting expression for the second possibility of function call.
Is there someone able to help me out with this problem? I also would appreciate a regular expression with explanation as I just started working with them.
The expression for the first function type:
R_Wrt_(\w+)\((\s*(V_)[a-zA-Z_0-9\[\] ]+)
Here is a regex that should fetch you expected results:
R_Wrt_(\w+)\((?:\((\w+)\)\s*)?\(?(\s*(V_)[a-zA-Z_0-9\[\]* ]+)\)*
See demo
The regex matches:
R_Wrt_ - literal R_Wrt
(\w+) - 1 or more English letters, digits or underscore (captured into Group 1)
\( - a literal opening parenthesis
(?:\((\w+)\)\s*)? - an optional non-capturing group (so as not to mess the groups) that matches...
\( - a literal opening parenthesis
(\w+) - 1 or more English letters, digits or underscore (captured into Group 2)
\)\s* - a literal closing parenthesis with optional whitespace
\(? - a literal optional opening parenthesis
(\s*(V_)[a-zA-Z_0-9\[\]* ]+) - a capturing group 3 (from your original regex) matching...
\s* - optional whitespace
(V_) - literal V_ (captured into Group 4)
[a-zA-Z_0-9\[\]* ]+ - 1 or more characters from the set
\)* - 0 or more literal closing parentheses.