Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matching brackets

Status
Not open for further replies.

ojw

Vendor
May 26, 2004
1
ZA
How do you match the beginning and end of a set of brackets in regular expressions.

For example, if I want to convert
{{{thisFunciton{{{}{}}}}}}
to
{{{thisFunciton{start{{}{}}end}}}}

Thanks

ojw

 
hmmm, regexes don't really work well in this situation. what you could do is the technique lisp programmers use to make sure their parentheses all match up: you start at zero, then add one for every '(' and subtract one for every ')', and you do this linearly, that is, start at the beginning and assign to each '(' or ')' the number that results from the addition or subtraction. then, to find the set of matching parens, you just find the first occurance of two equal numbers. this shouldn't be too hard:[tt]
my $text = "junk{hi{how{are{you}}}}junk";
my $bc = 0;
my @ba;
while ($text =~ /(\{|\}).*?(?=(\{|\}))/g)
{
if ($1 eq '{') {$bc += 1} else {$bc -= 1}
last if ($bc <= 0);
@ba[1..$bc] = map {$_ . $&} @ba[1..$bc];
}
@ba = map {$_ . '}'} @ba[1..$#ba];
print join(&quot;\n&quot;, @ba);
[/tt]
this may not be the absolute best way to do it, both in efficiency and logicality, but it will do what you're looking for. @ba will be an array where $ba[1] will be the data and brackets that occur first, $ba[2] will be the second set, &c.
if this were cleaned up a bit, it would make a nice addition to the perl cookbook.

stillflame out. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
You can predefine your search pattern and replace pattern and let the regex engine handle the details.

#!/usr/local/bin/perl
$find_this = '{{{thisFunciton{{{}{}}}}}}';
$put_this = '{{{thisFunciton{start{{}{}}end}}}}';

$find_this =~ s/$find_this/$put_this/;
print &quot;$find_this\n&quot;;

# or, you can escape all your braces with '\' or use hex values for your braces
# this is a little ugly, but it works.
$str = '{{{thisFunciton{{{}{}}}}}}';
$str =~ s/(\x7B{3}thisFunciton\x7B)(\x7B{2}\x7D\x7B\x7D{2})(\x7D{4})/$1start$2end$3/;
print &quot;\n$str\n&quot;;


happy motoring.




keep the rudder amid ship and beware the odd typo
 
hmmm. maybe i should have read your question more than just in my imagination before i answered it :p

first of all i'm assuming that the text your dealing with won't be known in detail before you search through it, that you'll only have the location within the brackets you're looking for, not the text that exists in them already. if my assumption is off, goBoating's code will work perfectly.

my code doesn't really do what you want yet, but it's a start. one thing it's failing to do is properly split up the data - an array is not the structure that should be used here. probably better would be a tree structure of some sort - root node being the full string, each subsequent level being the next outermost set of brackets, with a node for each set (as in '{{1}{2}}' becoming:[tt]
root: {{1}{2}}
/ \

first: {1} {2}[/tt]
(if that makes sense)). this is going to be similar to *ML (as in HTML and XML) parsing, so i'm going to do a bit more research before i commit to a method.

stillflame &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top