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. remove parenthese from function like string

remove parenthese from function like string

Scheduled Pinned Locked Moved Regular Expressions
c++hardwarebeta-testinghelp
7 Posts 2 Posters 22 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.
  • M Offline
    M Offline
    Michael Eckstein
    wrote on last edited by
    #1

    I have a similar string with a C++ function __X __X(" (whatever1)") alpha __X("whatever2") beta and I should remove the __X function and the corresponding parentheses, so the result should be "(whatever1)" alpha "whatever2" beta The argument to the __X function is a string embedded in "" and sometimes additionally embedded in () I'd appreciate any help, thanks in advance :-)

    M 1 Reply Last reply
    0
    • M Michael Eckstein

      I have a similar string with a C++ function __X __X(" (whatever1)") alpha __X("whatever2") beta and I should remove the __X function and the corresponding parentheses, so the result should be "(whatever1)" alpha "whatever2" beta The argument to the __X function is a string embedded in "" and sometimes additionally embedded in () I'd appreciate any help, thanks in advance :-)

      M Offline
      M Offline
      moxol
      wrote on last edited by
      #2

      Does this simple Regex.Replace work for you?

      string oldExp = @"__X(""(whatever1)"") alpha __X(""whatever2"") beta";
      string newExp = Regex.Replace(Regex.Replace(oldExp, @"__X\(""", @""""), @"""\)", @"""");

      M 1 Reply Last reply
      0
      • M moxol

        Does this simple Regex.Replace work for you?

        string oldExp = @"__X(""(whatever1)"") alpha __X(""whatever2"") beta";
        string newExp = Regex.Replace(Regex.Replace(oldExp, @"__X\(""", @""""), @"""\)", @"""");

        M Offline
        M Offline
        Michael Eckstein
        wrote on last edited by
        #3

        By whatever1 and whatever2 I meant that these strings are arbitrary, not just exactly whatever1(2). So it should work both for __X(" (Joe)") John __X("Silver") Doe with the result " (Joe)" John "Silver" Doe and __X(" (Jane)") Michael J. __X("Frank Sinatra") Fox with the result " (Jane)" Michael J. "Frank Sinatra" Fox I'd be happy even with replacing __X(" (Joe)") John was there for " (Joe)" John was there

        M 1 Reply Last reply
        0
        • M Michael Eckstein

          By whatever1 and whatever2 I meant that these strings are arbitrary, not just exactly whatever1(2). So it should work both for __X(" (Joe)") John __X("Silver") Doe with the result " (Joe)" John "Silver" Doe and __X(" (Jane)") Michael J. __X("Frank Sinatra") Fox with the result " (Jane)" Michael J. "Frank Sinatra" Fox I'd be happy even with replacing __X(" (Joe)") John was there for " (Joe)" John was there

          M Offline
          M Offline
          moxol
          wrote on last edited by
          #4

          Regex that I gave you will work whatever the string inside parentheses is. It focuses on removing the __X(" and "). You can use this one for any __X

          string newExp = Regex.Replace(Regex.Replace(oldExp, @"__\w+?\(""", @""""), @"""\)", @"""");

          M 1 Reply Last reply
          0
          • M moxol

            Regex that I gave you will work whatever the string inside parentheses is. It focuses on removing the __X(" and "). You can use this one for any __X

            string newExp = Regex.Replace(Regex.Replace(oldExp, @"__\w+?\(""", @""""), @"""\)", @"""");

            M Offline
            M Offline
            Michael Eckstein
            wrote on last edited by
            #5

            Is this possible to use in MS Visual Studio using 'Edit->Find and Replace->Replace in files' and checking 'Use regular expressions' and if so, then what should be in boxes 'Find what' and 'Replace with:' ? In fact, it is a revert replace from Find what: \("([^"]*)"(.*) Replace with: (__X("$1")$2

            M 1 Reply Last reply
            0
            • M Michael Eckstein

              Is this possible to use in MS Visual Studio using 'Edit->Find and Replace->Replace in files' and checking 'Use regular expressions' and if so, then what should be in boxes 'Find what' and 'Replace with:' ? In fact, it is a revert replace from Find what: \("([^"]*)"(.*) Replace with: (__X("$1")$2

              M Offline
              M Offline
              moxol
              wrote on last edited by
              #6

              Yes, it can be done from Find and Replace in two steps. Find: __\w+?\(" Replace: " Find: "\) Replace: "

              M 1 Reply Last reply
              0
              • M moxol

                Yes, it can be done from Find and Replace in two steps. Find: __\w+?\(" Replace: " Find: "\) Replace: "

                M Offline
                M Offline
                Michael Eckstein
                wrote on last edited by
                #7

                Hi moxol, thank you for your idea using two steps! I used this: Find: __X\("([^"]*)"(.*) Replace: "$1"q!@tr@$2 repeated n-times until no other replacement was made Then simply remove all q!@tr@) strings without regexp to remove the original closing parenthese, where q!@tr@ is any improbable string used as an identifier

                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