Find and Replace in Visual Studio with Regex - 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.

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.

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

regular expression for find replace modification

I wanted to use regular expressions in eclipse to adept code to a software update.
instead of
{$CFG->prefix}example1.xy
the code needs to be:
{example1}.xy
to work.
another example would be:
{$CFG->prefix}example2.foo
>
{example2}.foo
constant parts are : {$CFG->prefix}; .
i tried the following (i used whitespaces to make reading easier):
Find: \{\$CFG-\>prefix\} ([a-z]|[0-9])* \. ([a-z]|[0-9])*
Which will find the requested String. I struggle in replacing it.
i can use ,/1to store the result of the regex and use it in the replacement (right?) but i am not sure how i can modify/manipulate this result.
thanks for any help.
You can try
\{\$CFG-\>prefix\}([a-z0-9]*)\.
and replace with
{\1}.
I am not sure why you do have the whitespaces in your regex, I removed them.
the quantifier * should be inside your group, otherwise you will have only the last matched character in \1 and not the complete word.
Since you don't want to replace the last part, you don't need to match and replace it.
Try the following search and replace :
Find: \{\$CFG->prefix\}([a-z0-9]*)\.([a-z0-9]*)
Replace with : {\1}.\2
Using the above the following :
BECOMES
Here is a quick screen-cast to show this in action.
Changes made to the OP's Find reg-ex
In order to get the above find-replace to work, I had to make the following changes to the OP's find expression :
Removed whitespaces.
Moved the Greedy Match modifier inside the groups : i.e. ([...]*) instead of ([...])*
Corrected the character set : i.e [a-z0-9] instead of [a-z]|[0-9]
Introduced another Group which captures the part after the period. This however is not strictly needed but may be useful in some scenarios.

Find and replace string within a specific string with regex in visual studio

I'd like to replace automatically all the strings in my solution which are like this one
NotifyPropertyChanged("VariableParameter")
with this
NotifyPropertyChanged(Function() VariableParameter)
by "Quick Replace" in "Find and Replace" using a regular expression in Visual Studio 2010.
I have not the slightest idea how to do this when I have to keep each different variable parameter.
Try the following pattern and replacement.
Pattern: NotifyPropertyChanged\("{[^"]+}"\)
This matches your text, while escaping the parentheses. The {[^"]+} portion tags the contents (via the curly braces) and the [^"]+ bit matches any character that isn't a double-quote, one or more times.
Replacement: NotifyPropertyChanged(Function() \1)
This replaces the matched text and is fairly straightforward to understand. The \1 portion refers to the first (and only, in this example) tagged text from the pattern, which is the content between double-quotes.

Replacing char in a String with Regular Expression

I got a string like this:
PREFIX-('STRING WITH SPACES TO REPLACE')
and i need this:
PREFIX-('STRING_WITH_SPACES_TO_REPLACE')
I'm using Notepad++ for the Regex Search and Replace, but i'm shure every other Editor capable of regex replacements can do it to.
I'm using:
PREFIX-\('(.*)(\s)(.*)'\)
for search and
PREFIX-('\1_\3')
for replace
but that replaces only one space from the string.
The regex search feature in Notepad++ is very, very weak. The only way I can see to do this in NPP is to manually select the part of the text you want to work on, then do a standard find/replace with the In selection box checked.
Alternatively, you can run the document through an external script, or you can get a better editor. EditPad Pro has the best regex support I've ever seen in an editor. It's not free, but it's worth paying for. In EPP all I had to do was this:
search: ((?:PREFIX-\('|\G)[^\s']+)\s+
replace: $1_
EDIT: \G matches the position where the previous match ended, or the beginning of the input if there was no previous match. In other words, the first time you apply the regex, \G acts like \A. You can prevent that by adding a negative lookahead, like so:
((?:PREFIX-\('|(?!\A)\G)[^\s']+)\s+
If you want to prevent a match at the very beginning of the text no matter what it starts with, you can move the lookahead outside the group:
(?!\A)((?:PREFIX-\('|\G)[^\s']+)\s+
And, just in case you were wondering, a lookbehind will work just as well as a lookahead:
((?:PREFIX-\('|(?<!\A)\G)[^\s']+)\s+
You have to keep matching from the beggining of the string untill you can match no more.
find /(PREFIX-\('[^\s']*)\s([^']*'\))/
replace $1_$2
like: while (/(PREFIX-\('[^\s']*)\s([^']*'\))/$1_$2/) {}
How about using Replace all for about 20 times? Or until you're sure no string contains more spaces
Due to nature of regex, it's not possible to do this in one step by normal regular expression.
But if I be in your place, I do such replaces in several steps:
find such patterns and mark them with special character
(Like replacing STRING WITH SPACES TO REPLACE with #STRING WITH SPACES TO REPLACE#
Replace #([^#\s]*)\s to #\1_ server times.
Remove markers!
I studied a little the regex tool in Notepad++ because I didn't know their possibilities.
I conclude that they aren't powerful enough to do what you want.
Your are obliged to learn and use a programming language having a real regex capability. There are a number of them. Personnaly, I use Python. It would take 1 mn to do what you want with it
You'd have to run the replace several times for each space but this regex will work
/(?<=PREFIX-\(')([^\s]+)\s+/g
Replace with
\1_ or $1_
See it working at http://refiddle.com/10z