Regex substitution with Notepad++ - regex

I have a text file with several lines like these ones:
cd_cod_bus
nm_number_ex
cd_goal
And I want to get rid of the - and uppercase the following character using Notepad++ (I can also use other tool but if it doesn't get the problem more troublesome).
So I tried to get the characters with the following regex (?<=_)\w and replace it using \U\1\E\2 for the uppercasing trick but here is where my problems came. I think the regex is OK but once I click replace all I get this result:
cd_od_us
nm_umber_x
cd_oal
as you can see it is only deleting the match.
Do you know where the problem is?
Thanks.

The search regex has no capture groups, i.e. the \1 and \2 references in the replacement do not refer to anything.
Try this instead:
Search: _(\w)
Replace \U\1\E
There you have a capture group in the search part (the parenthesis around the \w) and the \1 in the replacement refers back to what was captured.

replace
_(.)
with
\U$1
will give you:
cdCodBus
nmNumberEx
cdGoal
and for your
I can also use other tool but if it doesn't get the problem more troublesome
I suggest you try vim.

Try this,
_(\w)
and replace with
\U\1
here's a screenshot

Related

Replace wrong xml-comments with Regex

I am dealing with a bunch of xml files that contain one-line-comments like this: // Some comment.
I am pretty sure that xml comments look like this: <!-- Some comment -->
I would like to use a regular expression in the Atom editor to find and replace all wrong comment syntax.
according to this question, the comment can be found with (?<=\s)//([^\n\r]*) and replaced with something like <!--$1-->. There must be an error somewhere since clicking replace button leaves the comment as is, instaed of replacing it. Actually I can't even replace it with a simple character.
The find and replace works with a different regex in the "Find" field:
Find: name.*
Replace: baloon
Is there anything I can write in the "Find" and "Replace" field to achieve this transformation?
Atom editor search and replace currently does not support lookbehind constructs, like (?<=\s). To "imitate" it, you may use a capturing group with an alternation between start of string, ^, and a whitespace, \s.
So, you may use
Find What: (^|\s)//([^\n\r]+)
Replace With: $1<!--$2-->
See the regex demo. NOTE \s may match newlines, so you may probably want to use (^|[^\S\r\n])//([^\n\r]+) to avoid matching across line breaks.
If you do not need to check for a whitespace, just remove that first capturing group and use a mere:
Find What: //([^\n\r]+)
Replace With: <!--$1-->
See another regex demo.

Using regular expresion in vscode to change some code

I editing a latex document where I want to change the references from \ref{eq:6.3.78} to (\ref{eq:6.3.78})
I tried to match first all the strings like \ref{SOMETHING} with \ref{.\*}
and then change to (\ref{.\*}) but this don't work. Why? Or how can archive this.
Manually it would take me more than 10 hours since is a complete math book.
You need to escape both the backslash and the curly braces. Try the following find and replace, in regex mode:
Find: \\ref\{[^}]+\}
Replace: ($0)
Demo
If for some reason the above does not work with VSCode, then you may try explicitly capturing the entire pattern:
Find: (\\ref\{[^}]+\})
Replace: ($1)
Note: If $1 does not work, then try using \\1 instead.

Need help using RegEx to Replace lines

I need to do a Find & Replace (NOT in Code, in the VB IDE or a Text Editor).
I need to take:
IsNumeric(CInt(whatevertextishere))
and change it to
IsNumeric(whatevertextwasthere)
I've tried:
IsNumeric\(CInt\(.*\)\)
for find, and IsNumeric($1) for the replace, the find works, but the replace does not. It simply replaces it with the literal text IsNumeric($1), instead of putting the matched text, which should be the "whatevertextishere".
What am I doing wrong?
You haven't defined a capture group in the regex (with unescaped parentheses):
IsNumeric\(CInt\((.*)\)\)

why with look behind regex, the replacement is not working well in Notepad++?

In Notepad++, for example if the search regex is
(?<latex>\$[^\$]*\$)(?=[\x{4e00}-\x{9fa5}])
and the replace is
~\g{latex}~
then the replacement is working properly.
But if the search regex contains look behind expression like
(?<=[\x{4e00}-\x{9fa5}])(?<latex>\$[^\$]*\$)(?=[\x{4e00}-\x{9fa5}])
then replace
~\g{latex}~
doesn't work in Notepad++, why???
Actually, I found out that your named backreference is the actual problem. As per the documentation, you need to use the syntax $+{name} for a named capture reference in the replace. So that one should work:
(?<=[\x{4e00}-\x{9fa5}])(?<latex>\$[^\$]*\$)(?=[\x{4e00}-\x{9fa5}])
And replace with:
~$+{latex}~
So the first regex you had should not be working properly, but replacing with literal ~g<latex>~. Even so, I can't really be sure here since I'm using an older version of N++ and the docs could be out of date.
Though I think that the simplest would be that you don't use the capture group. The below should work fine:
(?<=[\x{4e00}-\x{9fa5}])\$[^$]*\$(?=[\x{4e00}-\x{9fa5}])
And replace with ~$0~.

Replacing rgb from multiple lines with a one line version

I want to convert:
<r>240</r>
<g>240</g>
<b>240</b>
to:
rgb="240,240,240"
using Notepad++. I've tried
<r>(\d+)</r>$<g>(\d+)</g>$<b>(\d+)</b>
but it didn't work. I have about 20+ files containing multiple occurrences of this and would appreciate any help I can get. Can someone explain what I'm doing wrong please?
Notepad++ won't do a multiline regex. You first have to replace all of the newlines in the file. You can then do a regex replace with
regex: <r>(\d+)</r><g>(\d+)</g><b>(\d+)</b>
replace: rgb="\1,\2,\3"\r\n
Or you can break it up into more steps and do each line individually.
regex: <r>(\d+)</r>
replace: rgb="\1,
then
regex: <g>(\d+)</g>
replace: \1,
etc...
I just tested this on my system, I highlighted the tags then did search->Replace make sure you have checked Match Whole Word Only and Wrap around. worked for me replacing the text in multiple files.