Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching balanced parenthesis in Perl regex

I have an expression which I need to split and store in an array:

aaa="bbb{ccc}ddd" { aa="bb,cc" { a="b", c="d" } }, aaa="bbb{}" { aa="b}b" }, aaa="bbb,ccc"

It should look like this once split and stored in the array:

aaa="bbb{ccc}ddd" { aa="bb,cc" { a="b", c="d" } }
aaa="bbb{}" { aa="b}b" }
aaa="bbb,ccc"

I use Perl version 5.8 and could someone resolve this?

like image 208
meharo Avatar asked Nov 01 '11 23:11

meharo


People also ask

How do I match parentheses in Perl?

Whenever you use parentheses for grouping, they automatically work as memory parentheses as well. So, if you use /./, you'll match any single character (except newline); if you use /(.)/, you'll still match any single character, but now it will be kept in a regular expression memory.

How do you match brackets in regex?

Brackets indicate a set of characters to match. Any individual character between the brackets will match, and you can also use a hyphen to define a set. You can use the ^ metacharacter to negate what is between the brackets. You will often see ranges of the alphabet or all numerals.

What is \b in Perl regex?

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length.


1 Answers

Use the perl module "Regexp::Common". It has a nice balanced parenthesis Regex that works well.

# ASN.1
use Regexp::Common;
$bp = $RE{balanced}{-parens=>'{}'};
@genes = $l =~ /($bp)/g;
like image 177
Erik Aronesty Avatar answered Oct 08 '22 14:10

Erik Aronesty