Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

For a textbox form entry I would like to eliminate anything that starts with parenthesis:
(here is words or numbers or dates) information starts here etc...
So after my reg expression it will look like this:
information starts here etc...
My below attempt will look at any input that starts with a paranthesis then the paranthesis and all its words inside of it will be deleted:
if($input =~ /^\(\s*$\)/) { s/$1//; }
Please advise because it didnt work.

Retitled by g0n from 'Taking out info at beginning of textarea'.

Replies are listed 'Best First'.
Re: Stripping parenthesized test from beginning of a textarea string with regex
by holli (Abbot) on Sep 07, 2005 at 12:00 UTC
    You are matching only whitespace in parens (\s eq. whitespace). What you want is
    $input = "(here is words or numbers or dates) information starts here +etc..."; if($input =~ s/^\(([^\)]+)\)\s*// ) { print "deleted:$1\n"; print "rest:$input\n" } #output: # deleted:here is words or numbers or dates # rest:information starts here etc...


    holli, /regexed monk/
Re: Stripping parenthesized test from beginning of a textarea string with regex
by liverpole (Monsignor) on Sep 07, 2005 at 13:10 UTC
    holli is correct that a very straightforward way to search for text surrounded by parens, braces, brackets, etc., is to use the character class negation syntax [^...], as you want to stop capturing text to discard as soon as you find the matching parenthesis.

    However, your original example has a couple of other pitfalls:

    1. The '$' followed by "\)" is trying to match end-of-line followed by a parenthesis, which will never succeed.
    2. The attempt to use the result of a match in a new regex (s/$1//) is not advisable in a couple of ways.  First, you didn't actually -capture- the parenthetical expression (remember, your escaped parentheses \( ...\) are matching the open/close parentheses in the pattern, so you would need an extra set to capture those: (\(...\)); otherwise $1 won't be defined).  Secondly, even if you are correctly capturing, it's not a good idea to immediately use $1 without making an assignment first, as any other regex match will change its value.  Thus, it's better to write something like:
      if($input =~ /^(\([^\)]*\))/) { my $pattern = $1; s/$pattern//; }
    3. But since you're already doing the work of searching for the pattern, why not just do the substitutition at the same time?:
      if($input =~ s/^(\([^\)]*\))//) { # Successfully trimmed (...) at beginning of line } else { # Didn't match ^(...) }
    Notice that the last code segment is essentially what holli has suggested for a regex: "if($input =~ s/^\(([^\)]+)\)\s*// )".  The 2 differences are that he trims any whitespace after the final ')' with "\s*", and his expression only trims what's in the parentheses if it's one or more characters, because of the '+'.  (If you want to match empty parentheses as well, use '*' instead of '+').
Re: Stripping parenthesized test from beginning of a textarea string with regex
by pg (Canon) on Sep 07, 2005 at 13:30 UTC

    A design question. If all what you wanted to do is to strip the () at the beginning (or even couple of places), why not just have two (or more) textareas.

    That would be much clearer, both the user interface and code.

    You don't want your user to enter something that follows certain syntax. Whenever you can, try to make their lifes easier. That's the purpose of having GUI.

    When you write programs, try to be lazy and straight.