Solved

Regular Expression question - matching brackets

Posted on 2003-10-28
7
571 Views
Last Modified: 2008-02-01
Is there an easy way to do this with a perl regular expression?

I have an expression with matching brackets, but they can be nested, and I need to match the entire group.

For example, I have the string: MyTest{out{inner}er}blahblah

Is there an expression I can write that will match {out{inner}er}? If I search for the } I will get {out{inner} which is wrong. I could say find the second }, but then if I have {out{in{absoluteinner}ner}er} that breaks it.

Anyone have any idea? I'm sure it should be easy to do, but I cant figure it out.
0
Comment
Question by:edskee
7 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 9635315
It is not possible to match arbitrary nesting of parens or brackets with ordinary regular expressions. You need some auxiliary code to keep track of things like nesting levels.

To simply get the text enclosed by the outermost pair of braces, however, you should be able to do that with something like:

($result) = $string =~ /{(.*)}/;

The greedy match should consume intermediate closing braces until it finds the last one.

For the last word on regular expressions, you should take a look at Jeffry Friedl's book Mastering Regular Expressions.

http://www.oreilly.com/catalog/regex/

0
 
LVL 2

Accepted Solution

by:
ultimatemike earned 100 total points
ID: 9635524
Also, try using the Text::Balanced module. It was written for exactly what you're trying to do:


http://search.cpan.org/~jhi/perl-5.8.1/lib/Text/Balanced.pm
0
 
LVL 2

Author Comment

by:edskee
ID: 9636089
Well, I'm using the expression in a PHP script, so the perl module wont help me any... but I did write an extra function to check for the balanced braces, then match that number of braces before returning... so for anyone searching for an answer for this I accepted ultimatemike's answer, although jmcg's was closer to what I needed. Thanks.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 20

Expert Comment

by:jmcg
ID: 9636139
Mike,

I looked at Text::Balanced and thought "wow!". I believe it can be used to answer this problem, but I'd certainly appreciate (and  I bet Edskee would, too) a gentle introduction to this very complex module. Can you show a worked-out example for the question posed here by Edskee? Please?
0
 
LVL 2

Expert Comment

by:ultimatemike
ID: 9636666
I agree. I don't have access to the module right now - I'll have to hold off until tomorrow morning to give it a shot, but I'll try and get an example worked out to help out with future questions. :)
0
 
LVL 2

Expert Comment

by:ultimatemike
ID: 9637083
Turns out Text::Balanced is installed by default in ActivePerl :)





#!perl -w


use strict;

use Text::Balanced qw (
 
                        extract_bracketed
                      );



my $string = 'MyTest{out{inner}er}blahblah';

#The first parameter is the string to process
#The second is the brackets we're using. (You can have multiple kinds of nested brackets)
#The third is the regular expression for the prefix that we wish to ignore. In this case,
#it's checking if the bracket is after the start of the line and a string of alphanumeric
#characters.
my @result = extract_bracketed( $string, '{}', qr/^[A-Za-z0-9]*/);


print "The balanced text: $result[0]\n";
print "The dropped suffix: $result[1]\n";
print "The dropped prefix: $result[2]\n";



If that's still unclear, I'd be happy to try and clarify it further.
0
 
LVL 84

Expert Comment

by:ozo
ID: 9655801
$_ = "MyTest{out{inner}er}blahblah";
($re=$_)=~s/((\{)|(\})|.)/${{'{'=>'('}}{$2}\Q$1\E${{'}'=>')'}}{$3}/gs;
$result = (/$re/)[0];
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question