views:

625

answers:

3

Can anyone provide a regex for notepad++ for the below search and replace (conversion)

ADD  ( PRIMARY KEY (xxx) ) ;

to

ADD  PRIMARY KEY (xxx) ;

basically, removed a () around primary key expression. the value xxx is different among statements.

If not notepad++, I may also try the regex for vim or any shell script.

thanks a lot. Babu.

A: 

You don't need a reg-ex. Search for a '( ' and replace with a space. Then search for a ') ' and replace with nothing.

That is '([space]' and ')[space]'.

MichaelKay
The is '([space]' and ')[space]'
MichaelKay
thanks for ur quick reply... sorry i didn't mention.. there are other text which contains (, ) etc, which I don't want disturb..
bsreekanth
@bsreekanth then regex probably isn't going to help you. Regex doesn't understand nesting. It can't tell that the open and close parentheses belong together.
tloflin
+1  A: 

Search for:

ADD  \( PRIMARY KEY \((.+)\) \) ;

Replace with:

ADD  PRIMARY KEY (\1) ;
compie
Thank you so very much.. it worked great with a minor change .. the pattern to search for was ADD \( PRIMARY KEY \((.+)\) \) ;I'm sure you know better, still I didn't see the usage of 'd' in their manual (http://notepad-plus.sourceforge.net/uk/regExpList.php). thanks again for ur help.
bsreekanth
A: 

In perl assuming you read the line into $line

$line =~ s/([^(]+)\((.*)\)/$1$2/

would do the job. This also saves you from cutting an pasting ADD PRIMARY KEY ...(with the right number of spaces. What the regex is doing here is

a) Matching everything upto the first ( and capturing it into $1

b) Matching (

c) Matching the all the characters upto but not including the final ) and capturing it into $2.

d) Matching the trailing )

e) Replace the original string with $1$2.

Jasmeet