in reply to Regex operation optimisation

if you need to remove only matching (), your code won't do. Since '.' will match any character including ')' and since it is a greedy match, it will only eliminate the outmost parenthesis and (!)everything in between. Also the space before the parenthesis would have to match. You could use

s/\([^)]*\)//g #eliminate anything in parenthesis or s/\(([^)]*)\)/$1/g #remove matching parens but not the content
but you would have to repeat that regex until no match was found anymore. Except if you only have only one level of parenthesis, then one execution of the regex would be enough

removing characters can be done with one regex using a character class:

s/[%*()]//g;

even parenthesis if you don't mind that they don't match

you could simply use + instead of {1,}

UPDATE to correct a very silly mistake indeed: changed ? to +

Replies are listed 'Best First'.
Re^2: Regex operation optimisation
by number2 (Initiate) on Jul 02, 2010 at 11:12 UTC
    I think that ? means {0,1}. Possibly + would be more appropriate.

    Also, if you wish to eliminate all whitespace characters, which includes newlines, tabs, etc., then \s+ might be the way to go.
Re^2: Regex operation optimisation
by oldmanwillow (Novice) on Jul 02, 2010 at 12:31 UTC

    A non-greedy quantifier could be used to match only the first closing parenthesis.

    s/\(.*?\)//
      That turns "foo (bar (baz) qux) quux" into "foo  qux) quux". I'd be very surprised if the OP wants that to happen.