Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: data between brackets

by ww (Archbishop)
on Dec 06, 2007 at 22:25 UTC ( [id://655541]=note: print w/replies, xml ) Need Help??


in reply to data between brackets

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://655541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found