Swap two values based on pattern
-
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.
-
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.
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
-
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
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:
-
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:
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
-
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
Thanks Peter.
-
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.
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.)
-
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.)
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