Your regex, $value =~ /^.*\(.*)\)/; fails because you have an an unmatched closing parenthesis in the regex. Attempting to execute a short version of what may be your code,
my $value1 = "Total age recorded (TOTAGE)";
$value1 =~ /^.*\(.*)\)/;
print $value1 . "\n";
produces this:
Unmatched ) in regex; marked by <-- HERE in m/^.*\(.*) <-- HERE \)/ at ....
In other words, the regex engine balks when it finds a special character, the closing paren, unescaped, when it did not find an opening paren.
When you receive a message of that sort, it's valuable to those who would assist you, so it is well to include it in your post.
However, dealing only with the unmatched paren doesn't give you what you sought.
So, extending/explaining sh1tn's correct answer of
s/.+?\((.+?)\).*/$1/;
- The .+? matches "anything, one or more times, UNTIL..." the regex engine finds a literal open parenthesis in $value.
- The inner parens in sh1tn's formulation capture their content, in this case, "anything inside an opening paren, one or more times, until the regex engine finds closing paren, thus capturing (preserving) only "TOTAGE" in $1.
- Since he recommended a substitution, rather than a simple match, the portion of his code following the middle "/" effectively discarding the original content of $value and replaces it (substitutes) what it captured in $1.
You'll find more in the tutorial section, under Tutorials#Pattern-Matching-and-Regular-Expressions and while you may find Friedl's "Mastering Regular Expressions" more than you seek right now, but it's well worth the study.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.