How to remove an & in the following expressions
-
Hello everyone! since more than two decades programming with VC++ I never used Regular Expressions. But now I need... The solution in VS2022 contains some thousands line looking like: some_object.MyMethod(comma_separated_parameter_list, &SomeClass(argument)optiolal_parameter-list) What I need is removing the ampersand (&) from these code expressions. I found a way to search for such an expression. It is something like something like**
.\*MyMethod\\(.*, &SomeClass\\(
**But I could not find a working expression for Replace. So I need your help guys!
-
Hello everyone! since more than two decades programming with VC++ I never used Regular Expressions. But now I need... The solution in VS2022 contains some thousands line looking like: some_object.MyMethod(comma_separated_parameter_list, &SomeClass(argument)optiolal_parameter-list) What I need is removing the ampersand (&) from these code expressions. I found a way to search for such an expression. It is something like something like**
.\*MyMethod\\(.*, &SomeClass\\(
**But I could not find a working expression for Replace. So I need your help guys!
Typically, one would use capture grops and replacement expressions. In your case you might do
(.*MyMethod\(.*, )&(SomeClass\()/s\1\2
If there are many _SomeClass_es that you would like to replace, you might be able to use
(.*MyMethod\(.*, )&(\w*()/s\1\2
Be cautious! I Have not tested either of these, and any time you're experimenting with regular expression replacements, Bad Things can happen. Back up Early! Back up Often! This all assumes that you actually want to make replacements using Visual Studio, more information for which can be found [here:](https://learn.microsoft.com/en-us/visualstudio/ide/using-regular-expressions-in-visual-studio?view=vs-2022#:~:text=regular expression service.-,Capture groups and replacement patterns,-A capture group)
Keep Calm and Carry On
-
Hello everyone! since more than two decades programming with VC++ I never used Regular Expressions. But now I need... The solution in VS2022 contains some thousands line looking like: some_object.MyMethod(comma_separated_parameter_list, &SomeClass(argument)optiolal_parameter-list) What I need is removing the ampersand (&) from these code expressions. I found a way to search for such an expression. It is something like something like**
.\*MyMethod\\(.*, &SomeClass\\(
**But I could not find a working expression for Replace. So I need your help guys!
-
Visual Studio's "Replace in Files" option (Ctl-Shift-H) allows you to use regular expressions, for both the find and the replace parts, and even offers a drop down of selections for the more common situations.
Thank you Richard. I know about these options in Visual Studio. What I don't know is how to remove an & from a code line that I already found (using regular expressions) but leave all the other texts before and after this & unchanged!
-
Thank you Richard. I know about these options in Visual Studio. What I don't know is how to remove an & from a code line that I already found (using regular expressions) but leave all the other texts before and after this & unchanged!
I just tried a quick test and this works:
Expression in the Find box: &([a-z][A-Za-z]*)
Expression in the Replace box: $1The find expression looks for the ampersand followed by a lowercase letter, followed by zero or more alphabetics, and captures everything apart from the ampersand, which is outside the parentheses. The replace expression says to replace everything that was found (including the ampersand), with the text in the capture group, which is the portion in parentheses. You will most likely need to modify the find portion to match your exact requirements.
-
Typically, one would use capture grops and replacement expressions. In your case you might do
(.*MyMethod\(.*, )&(SomeClass\()/s\1\2
If there are many _SomeClass_es that you would like to replace, you might be able to use
(.*MyMethod\(.*, )&(\w*()/s\1\2
Be cautious! I Have not tested either of these, and any time you're experimenting with regular expression replacements, Bad Things can happen. Back up Early! Back up Often! This all assumes that you actually want to make replacements using Visual Studio, more information for which can be found [here:](https://learn.microsoft.com/en-us/visualstudio/ide/using-regular-expressions-in-visual-studio?view=vs-2022#:~:text=regular expression service.-,Capture groups and replacement patterns,-A capture group)
Keep Calm and Carry On
Thank you for pointing me out to capture groUps! Very interesting. However your suggestion with
Quote:
(.*MyMethod(.*, )&(SomeClass()/s\1\2
doesn't work. Example:
Quote:
original line:
qwerty.MyMethod(param1, param2, &SomeClass(param3), param4);
search for the parttern
.*MyMethod\\(.\*, &SomeClass\\(
finds the substring of the origin up to
SomeClass(
But trying to replace it (to remove the &) using your suggestion results to
(.*MyMethod(.*, )&(SomeClass()/s\1\2param3), param4);
:sigh:
-
I just tried a quick test and this works:
Expression in the Find box: &([a-z][A-Za-z]*)
Expression in the Replace box: $1The find expression looks for the ampersand followed by a lowercase letter, followed by zero or more alphabetics, and captures everything apart from the ampersand, which is outside the parentheses. The replace expression says to replace everything that was found (including the ampersand), with the text in the capture group, which is the portion in parentheses. You will most likely need to modify the find portion to match your exact requirements.
Thank you Richard! :thumbsup:
-
Hello everyone! since more than two decades programming with VC++ I never used Regular Expressions. But now I need... The solution in VS2022 contains some thousands line looking like: some_object.MyMethod(comma_separated_parameter_list, &SomeClass(argument)optiolal_parameter-list) What I need is removing the ampersand (&) from these code expressions. I found a way to search for such an expression. It is something like something like**
.\*MyMethod\\(.*, &SomeClass\\(
**But I could not find a working expression for Replace. So I need your help guys!
Thank you both Richard and k5054! The hint to look at "capture groups" was very useful! I've just tested: so find
(.*MyMethod\(.*, )&(SomeClass\(.*)
and replace with
$1$2
gives me what I need: the ampersand has been removed! :)