Perl regex help - matching parentheses


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl regex help - matching parentheses
# 1  
Old 06-19-2008
Perl regex help - matching parentheses

Let's say I'm trying to match potentially multiple sets of parentheses. Is there a way in a regular expression to force a match of closing parentheses specifically in the number of the opening parentheses?
For example, if the string is "((foo bar))", I want to be able to say "match any number of opening parentheses, then any number of non-parentheses, followed by the same number of closing parentheses as the opening parentheses found, no more and no less."
This will be used for things like extracting "((bar))" from "(foo ((bar)))".

Thanks in advance!
-cvp
# 2  
Old 06-19-2008
If you know the pattern you want to match that is what you would use:

Code:
my $str = '(foo ((bar)))';
my ($n) = $str =~ /(\Q((bar))\E)/;
print $n;

\Q escapes the meta meaning of the parenthesis and treats them literally.
# 3  
Old 06-19-2008
Thanks, but that wasn't my question; I already know how to escape parentheses.
My data is grouped in many variations of parentheses, sometimes doubled or tripled up. I wanted to know if there was a way to match all of those variations with one regex.
More examples:
extract "(bar)" from "(foo (bar))"
extract "(bar)" from "((foo (bar)) baz)"
extract "((foo))" from "(((foo)) bar)"
...etc.
The key thing here is matching the same number of closed parentheses as open parentheses. I just don't want to have to resort to using a stack or doing something like:
Code:
$string =~ /(\(+)[^)]*/;
$regex = ')' x length($1);
$match = $&;
if ($' =~ /$regex/) { $match .= $&; }
else { next; }
# etc.

# 4  
Old 06-19-2008
I can't think of how to do all that with a single regexp. Maybe someone else can.
# 5  
Old 06-25-2008
Apparently, someone else can't.
Thanks anyway.

Eat a peach.
# 6  
Old 02-14-2009
You can try something like:

"([(]+\d+[)]+)"

this will detect the following examples:
((1) or ((1)) or ((((23)))

This is how I manage parentesis.
Hope this help!
# 7  
Old 02-16-2009
Code:
#!/usr/bin/perl
$str="(one(((two)))abc)a((b))c(d)";
@arr=$str=~/\(+/g;
@tmp= sort {length($b) <=> length($a)}@arr;
# if would like to find the deepest '()', uncomment below part
#$num=length($tmp[0]);
#$exp1=qr/\({$num}[^\(\)]*\){$num}/;
#$str=~/$exp/;
# change below number can find whatever levels deep '()'
$num=1;
$exp2=qr/\({$num}[^\(\)]*\){$num}/;
@res=$str=~/$exp2/g;
print join "\n", @res;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Regex matching column awk

Hi all, I want to extract rows with the pattern ALPHANUMERIC/ALPHANUMNERIC in the 2nd column. I dont wan rows with more than 1 slash or without any slash in 2nd column. a a/b b a/b/c c a/b//c d t/y e r f /f I came up with the regex grep '\/$' file a a/b b a/b/c d t/y (3 Replies)
Discussion started by: jianp83
3 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. UNIX for Dummies Questions & Answers

Regex matching with grep -l

I am trying to find patterns in files using grep -l -e. I specifically am searching for abc. I want any file that has abc in it, but not just the letters abc. I am searching for a pattern a followed by b followed by c. I have tried egrep -l and also I have tried the following: grep -el... (2 Replies)
Discussion started by: newbie2010
2 Replies

4. Shell Programming and Scripting

Perl: Regex, string matching

Hi, I've a logfile which i need to parse and get the logs depending upon the user input. here, i'm providing an option to enter the string which can be matched with the log entries. e.g. one of the logfile entry reads like this - $str = " mpgw(BLUESOAPFramework):... (6 Replies)
Discussion started by: butterfly20
6 Replies

5. Shell Programming and Scripting

Perl Regex matching multiple lines

I need a way to extract data from X 4T Solution 21 OCT 2011 37 .00 to account 12345678 User1 user2 X 4T Solution Solution Unlimited 11 Sep 2009 248 .00 to account 87654321 user3 user4 I need it to extract 'X' '37.00' and account number 12345678. I have extracted above stuff... (3 Replies)
Discussion started by: chakrapani
3 Replies

6. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

7. Shell Programming and Scripting

Perl - Parentheses & Context confusion

Hi Expert, Could you please explain why below two perl code get different result? Thanks a lot. sub test{ return (2,3,4,5,6,3,4,50); } ($a,$b)=(test); # 3,6 ($a,$b)=test; # 2,3 (2 Replies)
Discussion started by: summer_cherry
2 Replies

8. Shell Programming and Scripting

sed - print only matching regex

Hi folks, Lets say I have the following text file: name, lastname, 1234, name.lastname@test.com name1, lastname1, name2.lastname2@test.com, 2345 name, 3456, lastname, name3.lastname3@test.com 4567, name, lastname, name4.lastname4@test.com I now need the following output: 1234... (5 Replies)
Discussion started by: domi55
5 Replies

9. Shell Programming and Scripting

REGEX: Matching Null?

I'm using the URL Regex feature of Squid for allowing sites via a list of regex strings to match allowed domains. The regex was actually copied from our previous proxy solution and it seemed to "just work". But, we've recently discovered that some domains (likely due to virtual hosts or host... (2 Replies)
Discussion started by: deckard
2 Replies

10. Shell Programming and Scripting

Parentheses in perl find/replace

I'm trying to use the following command to do a batch find and replace in all commonly named files through a file hierarchy find . -name 'file' |xargs perl -pi -e 's/find/replace/g' which works fine except for a substitution involving parenthesis. As a specific example I'm trying to sub... (3 Replies)
Discussion started by: Jeffish
3 Replies
Login or Register to Ask a Question