2

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.

4 Answers 4

4

Search for:

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

Replace with:

ADD  PRIMARY KEY (\1) ;
1
  • 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 (notepad-plus.sourceforge.net/uk/regExpList.php). thanks again for ur help.
    – bsr
    Commented Apr 13, 2010 at 22:16
0

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.

0

Try this whereas the x is the last word in xxx, you can change it to a dot too

Search what: \s\s\K\(|x\) \K\)
Replace with: nothing

If didn't work, Please share your complete example of string
And desired output ......

-1

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]'.

3
  • The is '([space]' and ')[space]'
    – Michaelkay
    Commented Apr 13, 2010 at 18:58
  • thanks for ur quick reply... sorry i didn't mention.. there are other text which contains (, ) etc, which I don't want disturb..
    – bsr
    Commented Apr 13, 2010 at 19:02
  • @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
    Commented Apr 13, 2010 at 19:06

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