Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. How to remove an & in the following expressions

How to remove an & in the following expressions

Scheduled Pinned Locked Moved Regular Expressions
c++helptutorial
8 Posts 3 Posters 25 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Victor Nijegorodov
    wrote on last edited by
    #1

    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!

    K L V 3 Replies Last reply
    0
    • V Victor Nijegorodov

      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!

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      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

      V 1 Reply Last reply
      0
      • V Victor Nijegorodov

        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!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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.

        V 1 Reply Last reply
        0
        • L Lost User

          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.

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #4

          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!

          L 1 Reply Last reply
          0
          • V Victor Nijegorodov

            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!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I just tried a quick test and this works:

            Expression in the Find box: &([a-z][A-Za-z]*)
            Expression in the Replace box: $1

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

            V 1 Reply Last reply
            0
            • K k5054

              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

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #6

              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:

              1 Reply Last reply
              0
              • L Lost User

                I just tried a quick test and this works:

                Expression in the Find box: &([a-z][A-Za-z]*)
                Expression in the Replace box: $1

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

                V Offline
                V Offline
                Victor Nijegorodov
                wrote on last edited by
                #7

                Thank you Richard! :thumbsup:

                1 Reply Last reply
                0
                • V Victor Nijegorodov

                  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!

                  V Offline
                  V Offline
                  Victor Nijegorodov
                  wrote on last edited by
                  #8

                  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! :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups