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. Swap two values based on pattern

Swap two values based on pattern

Scheduled Pinned Locked Moved Regular Expressions
regextutorialquestion
7 Posts 2 Posters 21 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.
  • B Offline
    B Offline
    biop codeproject
    wrote on last edited by
    #1

    To cut down the detail, here is my input: [char](15) [varchar](23) So the first field is a square bracket '[', a string and a close bracket ']'. Next is a regular bracket '(', a number, and a close bracket ')'. I want to use Emacs to replace so here is the result: 15 char 23 varchar So take out all those bracket or square bracket. Number goes first, a space and the string. How to do it? I know how to swap values, but adding the pattern of string, I don't know.

    P B 2 Replies Last reply
    0
    • B biop codeproject

      To cut down the detail, here is my input: [char](15) [varchar](23) So the first field is a square bracket '[', a string and a close bracket ']'. Next is a regular bracket '(', a number, and a close bracket ')'. I want to use Emacs to replace so here is the result: 15 char 23 varchar So take out all those bracket or square bracket. Number goes first, a space and the string. How to do it? I know how to swap values, but adding the pattern of string, I don't know.

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      Use (automatically) numbered capture groups: Matching regex

      \[(\S*)\]\((\d*)\)

      will capture a string of non-whitepace between [ and ] into $1 and the following number in ( ) into $2. Your replacement string is then

      $2 $1

      Peter

      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

      B 1 Reply Last reply
      0
      • P Peter_in_2780

        Use (automatically) numbered capture groups: Matching regex

        \[(\S*)\]\((\d*)\)

        will capture a string of non-whitepace between [ and ] into $1 and the following number in ( ) into $2. Your replacement string is then

        $2 $1

        Peter

        Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

        B Offline
        B Offline
        biop codeproject
        wrote on last edited by
        #3

        Sorry Peter, does not work in Emacs 23.3.1 Windows version. When typing in the search string, Emacs does not complain, so it seems like working. But it just does not do the replace. :doh:

        P 1 Reply Last reply
        0
        • B biop codeproject

          Sorry Peter, does not work in Emacs 23.3.1 Windows version. When typing in the search string, Emacs does not complain, so it seems like working. But it just does not do the replace. :doh:

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          I'm not familiar with Emacs (in the last 25 years, anyway). It seems to use a very different regex engine from the ones we meet here (in .NET languages, PHP, Java and so on). You might do better to ask in an Emacs forum. Sorry I can't help any more. Peter

          Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

          B 1 Reply Last reply
          0
          • P Peter_in_2780

            I'm not familiar with Emacs (in the last 25 years, anyway). It seems to use a very different regex engine from the ones we meet here (in .NET languages, PHP, Java and so on). You might do better to ask in an Emacs forum. Sorry I can't help any more. Peter

            Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

            B Offline
            B Offline
            biop codeproject
            wrote on last edited by
            #5

            Thanks Peter.

            1 Reply Last reply
            0
            • B biop codeproject

              To cut down the detail, here is my input: [char](15) [varchar](23) So the first field is a square bracket '[', a string and a close bracket ']'. Next is a regular bracket '(', a number, and a close bracket ')'. I want to use Emacs to replace so here is the result: 15 char 23 varchar So take out all those bracket or square bracket. Number goes first, a space and the string. How to do it? I know how to swap values, but adding the pattern of string, I don't know.

              B Offline
              B Offline
              biop codeproject
              wrote on last edited by
              #6

              Well I find out the result myself using sed! Well with the help from using sed. After some search, I find out I can do the following: 1. Find bracket [ or ] - I need to escape this in search string (\[ or \]) - To search for [abc] -> \[\([a-z]*\)\] 2. Find parentheses ( or ) - I do not escape this in search string - To search for (123) -> (\([0-9]*\)) 3. Use simple [a-z] or [0-9] instead of \S, \w, \d Therefore I have: echo [abc](123) | sed -e "s/\[\([a-z]*\)\](\([0-9]*\))/\2 \1/" Output: 123 abc So in emacs, I do the following: 1. M-x regular-regexp 2. \[\([a-z]*\)\](\([0-9]*\)) 3. \2 \1 (Make sure there is a space between \2 and \1.)

              P 1 Reply Last reply
              0
              • B biop codeproject

                Well I find out the result myself using sed! Well with the help from using sed. After some search, I find out I can do the following: 1. Find bracket [ or ] - I need to escape this in search string (\[ or \]) - To search for [abc] -> \[\([a-z]*\)\] 2. Find parentheses ( or ) - I do not escape this in search string - To search for (123) -> (\([0-9]*\)) 3. Use simple [a-z] or [0-9] instead of \S, \w, \d Therefore I have: echo [abc](123) | sed -e "s/\[\([a-z]*\)\](\([0-9]*\))/\2 \1/" Output: 123 abc So in emacs, I do the following: 1. M-x regular-regexp 2. \[\([a-z]*\)\](\([0-9]*\)) 3. \2 \1 (Make sure there is a space between \2 and \1.)

                P Offline
                P Offline
                Peter_in_2780
                wrote on last edited by
                #7

                Good one. Now we all know a bit more about a different regex engine (Emacs/sed). Cheers, Peter

                Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                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