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 '+').

In reply to Re: Stripping parenthesized test from beginning of a textarea string with regex by liverpole
in thread Stripping parenthesized test from beginning of a textarea string with regex by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.