Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to find and replace caret open_parentheses some_content close_parentheses (i.e.^(.*)) with caret open_curly_bracket the_same_content close_curly_bracket (i.e. ^{.*} but I have issues.

1) I do not understand how to preserve the matched wildcard content .*

2) I do not see how to formulate the escape sequence to specify curly brackets and parentheses.

I have been going through this page for quite a bit and have attempted the following

%s/^(*)/^{*} /gc
%s/^(.*)/^{.*} /gc
%s/^/(.*/)/^{.*} /gc
share|improve this question
3  
What carrots? Did you mean caret? –  kinokijuf Jul 22 '12 at 20:24
    
Yes i did mean caret! –  rudolph9 Jul 22 '12 at 20:28
add comment

2 Answers 2

up vote 4 down vote accepted

All of those characters have special functionality depending if an escape character precedes them. This depends on the character whether an escape sequence preceding the character with special functionality utilises it (i.e. a ^ executes special functionality, a \^ does not, where a ( does not execute special functionality, and a \( does.

The expression you are looking for is as follows

s/\^(\([^)])\))/\^\{\1\}/g

The expression literally state, match a caret followed by an open_parenthesis followed by any character except a close_parenthesis followed by a close_parenthesis and replace with a a caret followed by an open_curly_bracket followed by the content found when searching for any_character_except a close_parenthesis (i.e. the content between \( and \)) followed by a close_curly_bracket

Note: other approaches exist as well

share|improve this answer
    
How do I say the first occurrence of the of the ')' close_parentheses? –  rudolph9 Jul 22 '12 at 16:18
    
The regex works but matched the last close_parentheses before the newline character. –  rudolph9 Jul 22 '12 at 16:24
1  
There are two ways to do that. One is to replace * by \{-} to match as few . as possible. See :help /\{-. The other is to replace . by [^)] to match anything not a ). See :help /[]. –  garyjohn Jul 22 '12 at 16:54
    
Yeah, I forgot about the greedy match thing. Thanks @kinokijuf for editing my answer! –  fluffy Jul 23 '12 at 17:08
    
@fluffy but i only corrected several typos –  kinokijuf Jul 23 '12 at 18:11
show 3 more comments

Are you really looking for the caret character?
The caret usually means to start looking for the pattern at the beginning of a line.

Assuming you are...

some text ^( more text ) and more ^( more text ) and more

--AND-- you what it changed to:

some text ^{ more text } and more ^{ more text } and more

Then, use:

:s-\^(([^)]+))-^{\1}-gc

:s      the substitute command
-       start pattern, use - instead of / for clarity
\^      look for the ^ char, need to escape
(       followed by (
\(      start capture
[^)]\+  one or more chars except )
\)      end capture
)       followed by )
-       start replace, use - instead of / for clarity
^{      replace ^( with ^{
\1      replace with the captured text
}       replace ) with }
-       start flags, use - instead of / for clarity
gc      confirm replace for each occurance in the line

If you want the search and replace to span the whole file, then use:

:%s-\^(([^)]+))-^{\1}-gc

The % means the whole file.

share|improve this answer
    
My assumption in this question is that he's looking to replace regexps with other regexps, using a regexp. Always tricky. :) –  fluffy Jul 23 '12 at 17:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.