Changing Parantheses to Square Brackets With Regular Expressions - regex

Under Visual Studio 2019 I am trying to replace Parantheses with Square Brackets
For example: fld(126) to fld[126]
Using this regular expression
fld[\(][0-9]*[\)]
matches good what I look for in the code.
But \2 replaces everything between parentheses with '\2' instead of leaving what exists there before.
Any help would be appreciated...

Your example RegEx shows you only want to change the brackets for the function fld.
Use
Find: fld\((\d*)\)
Replace: fld[$1]
If the content between the brackets can be more than numbers use
Find: fld\(([^)]*)\)
Replace: fld[$1]

Try the following find and replace, in regex mode:
Find: ([^(\s]+)\(([^)]+)\)(?!\S)
Replace: $1[$2]
Demo
Here is an explanation of the regex pattern:
([^(\s]+) match AND capture the leading 'fld' term, in $1
this is given by all non '(' and whitespace characters
\( match a literal (
([^)]+) match AND capture the content inside parentheses, in $2
\) match a literal )
(?!\S) assert that what follows the closing ) is a boundary or whitespace

Related

RegEx for replacing alphanumeric chars in Notepad++ [duplicate]

This question already has an answer here:
Notepad++ regular expression replace
(1 answer)
Closed 3 years ago.
I want to upgrade a project from one version to another so, that in need to change thousands of lines with same pattern.
Example:
From this
$this->returnData['status']
To this
$this->{returnData['status']}
By using following regex i found all the matches but unable to replace with braces.
->[a-zA-z]{5,15}\['[a-zA-z]{5,15}'\]
I used following to replace
->\{[a-zA-z]{5,15}\['[a-zA-z]{5,15}'\]\}
Try using the following find and replace, in regex mode:
Find: \$this->([A-Za-z]{5,15}\['[A-Za-z]{5,15}'\])
Replace: $this->{$1}
Demo
The regex matches says to:
\$this-> match "$this->"
( then match and capture
[A-Za-z]{5,15} 5-15 letters
\[ [
'[A-Za-z]{5,15}' 5-15 letters in single quotes
\] ]
) stop capture group
The replacement is just $this-> followed by the first capture group, but now wrapped in curly braces.
If you want to just match the arrow operator, regardless of what precedes it, then just use this pattern, and keep everything else the same:
->([A-Za-z]{5,15}\['[A-Za-z]{5,15}'\])
You could also use negated character classes and use \K to forget the match. In the replacement use $0 to insert the full match surrounded by { and }
Match:
->\K[^[\n]+\['[^\n']+']
That will match
-> match ->
\K Forget what was matched
[^[\n]+ Match not [ or newline 1+ times or exactly [a-zA-Z]{5,15} instead of [^[\n]+
\[ Match [
'[^\n']+' Match from ', 1+ times not ' or newline and then ' or exactly [a-zA-Z]{5,15} instead of [^\n']+
] Match literally
Regex demo
Replace with:
{$0}
Result:
$this->{returnData['status']}
The exacter version would look like:
->\K[a-zA-Z]{5,15}\['[a-zA-Z]{5,15}']
Regex demo
Note that [a-zA-z] matches more than [a-zA-Z]

Regex to remove all parentheses except most external ones

I have been trying and reading many similar SO answers with no luck.
I need to remove parentheses in the text inside parentheses keeping the text. Ideally with 1 regex... or maybe 2?
My text is:
Alpha (Bravo( Charlie))
I want to achieve:
Alpha (Bravo Charlie)
The best I got so far is:
\\(|\\)
but it gets:
Alpha Bravo Charlie
You can use a regex like this:
(\(.*?)\((.*?)\)
With this replacement string:
$1$2
Regex demo
Update: as per ııı comment, since I don't know your full sample text I provide this regex in case you have this scenario
(\([^)]*)\((.*?)\)
Regex demo
From your post and comments, it seems you want to remove only the inner most parenthesis, for which you can use following regex,
\(([^()]*)\)
And replace with $1 or \1 depending upon your language.
In this regex \( matches a starting parenthesis and \) matches a closing parenthesis and ([^()]*) ensures the captured text doesn't contain either ( or ) which ensures it is the innermost parenthesis and places the captured text in group1, and whole match is replaced by what got captured in group1 text, thus getting rid of the inner most parenthesis and retaining the text inside as it is.
Demo
Your pattern \(|\) uses an alternation then will match either an opening or closing parenthesis.
If according to the comments there is only 1 pair of nested parenthesis, you could match:
(\([^()]*)\(([^()]*\)[^()]*)\)
( Start capturing group
\( Match opening parenthesis
[^()]* Match 0+ times not ( or )
) Close group 1
\( Match
( Capturing group 2
\([^()]*\) match from ( till )
[^()]* Match 0+ times not ( or )
) close capturing group
\) Match closing parenthesis
And replace with the first and the second capturing group.
Regex demo

Regular expressions in Sublime Text 3

I am trying to make a regular expression that replaces the content of the texts in parentheses.
I have used the following regular expression:
"([A-Za-z ]*)"
But as you can see in the following image does not work:
Thank you and greetings.
Remove the double quotes from your expression and escape the parentheses:
\([A-Za-z ]*\)
Details:
\( - a literal (
[A-Za-z ]* - zero or more ASCII letters or spaces
\) - a literal ).
The unescaped (...) form a capturing group that stores a submatch in the memory buffer that can be used later during matching or replacement via backreferences.

Can notepad++ regex find a string and replace with a new string that contains the found string

I have the following text:
.clk,
.rst_b,
.phase_in
I want the follwing:
.clk(clk),
.rst_b(rst_b),
.phase_in(phase_in)
That is-
find a string starting with a period and excluding a trailing comma that may or may not be present.
Append the string, excluding the period to the found string inside parenthesizes.
Notepad++ has the function "find and replace" not "find and append" therefore step two could be written as follows - replace the string with a copy of itself followed by a copy of itself enclosed in parenthesizes.
Step one is completed by \.\w+. Any thoughts on step 2?
Thanks!
Yes you can! Use capture groups to refer to the matched word in your replacement.
Replace \.(\w+) with .\1\(\1\).
The parentheses mean that we want to keep a reference to \w+. We access that reference with \1 in the replacement.
You can use the following regular expression. This captures word characters following the dot.
Find: ^\.(\w+)
Replace: .\1\(\1\)
Explanation:
^ # the beginning of the string
\. # '.'
( # group and capture to \1:
\w+ # word characters (a-z, A-Z, 0-9, _) (1 or more times)
) # end of \1
In the replacement, we use \1 to reference what was matched and captured by capturing group #1. Note: You need to escape the parentheses in the replacement to actually display them.
use \1 find \.(\w+) and replace with \.\1\(\1\)
You can use a simple regex like this:
Find what: \.([^,]+)
Replacement: .$1\($1\)
Online demo
Make sure regex expression radio button is checked and dot matches newline is unchecked.
Pattern explanation:
\. '.'
( group and capture to \1:
[^,]+ any character except: ',' (1 or more times)
) end of \1

Regular expression to match a regular expression inside square brackets

I have a string that contains a regular expression within square brackets, and can contain more than 1 item inside square brackets. below is an example of a string I'm using:
[REGEX:^([0-9])*$][REGEXERROR:That value is not valid]
In the example above, I'd like to match for the item [REGEX:^([0-9])*$], but I can't figure out how.
I thought I'd try using the regular expression \[REGEX:.*?\], but it matches [REGEX:^([0-9] (ie; it finishes when it finds the first ]).
I also tried \[REGEX:.*\], but it matches everything right to the end of the string.
Any ideas?
Suppose you are using PCRE, this should be able to find nested brackets in regular expressions:
\[REGEX:[^\[]*(\[[^\]]*\][^\[]*)*\]
This technique is called unrolling. The basic idea of this regex is:
match the starting brackets
match all characters that are not brackets
match one brackets
match all trailing characters that are not brackets
then repeat 3 and 4 until the last closing bracket comes
Explanation with free-space:
\[ # start brackets
REGEX: # plain match
[^\[]* # match any symbols other than [
( # then match nested brackets
\[ # the start [ of nested
[^\]]* # anything inside the bracket
\] # closing bracket
[^\[]* # trailing symbols after brackets
)* # repeatable
\] # end brackets
Reference: Mastering Regular Expression