Using regular expresion in vscode to change some code - regex

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.

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.

How to replace specific string with regular expression in sublime text?

I have a project where there is a function named get_descriptioin('string_value') almost every file.
So I want to keep only string_value and remove get_description() from all files at once. I tried several ways but I can only select and remove phrases like get_descriptioin (or get_descriptioin('string_value'), not just the specific ones that I want.
Try the following find and replace, in regex mode:
Find: \bget_description\((.*?)\)
Replace: $1
Demo

Find and Replace in Visual Studio with Regex

I have an XSL file that I need to replace all the classes with a single class. Example:
class="class1", class="anotherclass", class="yoyoclass"
and replace all the values between the quotes with NewClass.
Find:
class=".*?"
Replace:
class="NewClass"
Explanation:
The replacement is self-explanatory, since it is the same for all classes. The only part of the find regex which needs explanation is the .*? term. Here, the ? tells the regex to stop consuming upon hitting the first closing quotes. Try removing the ? and you will see that the regex will become greedy and match everything until the last quote.

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~.

Regex substitution with Notepad++

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