How to search and replace with a capture group in VBA - regex

I want to use a regular expression in VBA to do a search, capture part of it, and then use that part in the replacement. For example, I want to run the search and replace on these lines:
(a4a)
(aHa)
And get a result of:
(b4b)
(bHb)
How do I capture the 2nd character and use it again in the replacement?

In VBA, capture parts of the search with parenthesis () and use them in replacement with $ and the number of the capture occurrence. Note, normal parenthesis need to be escaped, which is the opposite of vim.
So in this case:
searchPattern = "\(a(.)a\)"
replacement = "(b$1b)"

Related

find and replace regex [duplicate]

I am using the Kate editor. Here is a minimal example that shows my problem:
I have a file with a bunch of occurrences of:
\command{stuff}
where stuff is some arbitrary string of letters. I want to replace this with
\disobey{stuff}
where stuff is unchanged. The regular expression:
\\command\{[a-zA-Z]*\}
matches such expressions. So I pull the replace dialog with CTRL-r, and enter
Find: \\command\{[a-zA-Z]*\}
Replace: \\disobey\{\1\}
So in the document, an actual instance is say,
\command{exchange}
and when I hit the replace button is changed to
\disobey{1}
In the Kate documentation: Appendix B: Regular Expressions, \1 should match the first pattern used. Is this indeed the correct syntax? I have also tried $1, #1, and various other things.
Here is a quote directly from the documentation:
The string \1 references the first sub pattern enclosed in parentheses
So you need to put [a-zA-Z]* in a capturing group, like ([a-zA-Z]*).
Find: \\command\{([a-zA-Z]*)\}
Replace: \\disobey\{\1\}
Wrap the value with ( ) to capture it as a group, so you can use it in your replace
So change your find regex like this:
\\command\{([a-zA-Z]*)\}
and you should do fine.

Find and replace parts of matched string in Notepad++

I have something in a text file that looks like '%r'%XXXX, where the XXXX represents some name at the end. Examples include '%r'%addR or '%r'%removeA. I can match these patterns using regex '%r'%\w+. What I would like to replace this with is '{!r}'.format(XXXX). Note that the name has to stay the same in the replace so I'd get '{!r}.format(addR) or '{!r}.format(removeA). Is there a way to replace parts of the matched string in this way while retaining the unknown variable name pulled out with \w+ in the regex search?
I'm specifically looking for a solution using the find and replace features in Notepad++.
You can use
'%r'%(\w+)
and replace with '{!r}.format\(\1\)
The '%r'%(\w+) pattern contains a pair of unescaped parentheses that create a capturing group. Inside the replacement pattern, we use a \1 backreference to restore that value.
NOTE: The ( and ) in the replacement must be escaped because otherwise they are treated as Boost conditional replacement pattern functional characters.
See more on capturing groups and backreferences.
Search on:
'%r'%(XXXX)
Replace with:
Whatever You like \1
\1 will match the first set of grouping parentheses.

Adding a space between javascript keywords and parenthesis

I want to do a bulk insert of a space between certain JavaScript keywords and the opening parenthesis immediately following them.
The regex I was going to use, is:
(if|function|instanceof|return)\(
But I can't replace it with a fixed string like
this (
since it would overwrite the individual keywords.
How do I find just the paren in that regex, such that I can replace it with:
[blank space](
I'd want to use a single regex instead of one search expression for each individual keyword.
Use $N to refer to the string matched by the Nth capture group:
newstr = str.replace(/(if|function|instanceof|return)\(/g, '$1 (');

replace regular expression in sublime text

I have application where few labels are written like
ui-label-Display Not Masked
Now I want to replace it by
ui-label-Display_Not_Masked
so i have written search regex by
ui-label-(\w+ )*
This searches all expression but I am not able to create a expression to replace this text as required.
I have written one regex
$1_
which replaces
ui-label-Display Not Masked
by
ui-label-Display Not_Masked
This cannot be done with a single regex in a single iteration.
You have two choices:
Replace (ui-label-\w+) (note the space at the end) with $1_ until it no longer matches anything.
Make a looong regex with as many capture groups as necessary, i.e. (ui-label-\w+) (?:(\w+)(?: (\w+))?)? and replace with $1_$2_$3.

unintended replacement in Regular Expression

I have hundreds lines of text like this:
gi|393925858|gb|AGTA02071966.1| 0000000739 . G A 121.20 PASS NS=74:AN=2:DP=8448 GT:DP:GQ:EC:SG 0/1:262:144:116:R
I wanted to ONLY replace the colon with semicolon in this portion "NS=74:AN=2:DP=8448" of the line. Here is how I matched and replaced it:
if re.match(r'.*NS=\d+(:)AN=\d(:)DP=\d+.*', line):
print line.replace(':', ";")
I thought I just replaced the matched pattern in all lines, but it replaced EVERY colon with semicolon in all lines, is there a way to specify just the matched ones, or my regular expression was incorrect? Thanks.
The way to do this is to use the full regex in the replacement, using capture groups (parentheses) to capture the digits you want to keep.
So your search term is this:
NS=(\d+):AN=(\d+):DP=(\d+)
And your replace term is this:
NS=\1;AN=\2;DP=\3
Note that in the replacement, the \1 will be filled in with what the first capture group (parens) captured from the original text.