I ran into something which I thought should be easy, but has stumped me. I'm trying to match data within a round bracket. For example, take the following string:

$var = "(this is a good idea!), No it's not";

Now here's the regex I'm using to extract this data:

if ( $var =~ m/^\((.*)\),(.*)$/ ) { @args = ($1, $2); }

But this doesn't seem to work. For some reason It's not escaping the brackets correctly. I've tried this as well...

if ( $var =~ m/^[(](.*)[)],(.*)$/ ) { @args = ($1, $2); }

But no good. when I only escape the first bracket with [(], I seem to be getting somewhere, but as soon as I finish it with [)], it's broken again. Many thanks and blessing be upon you.

~~UPDATE~~

Alright, lemme try this again. The best way I can explain this is with some working (or should be working) code:

#!/usr/bin/perl -w use strict; use Data::Dumper; my $data = "U|(Memory is incorrectly balanced between the NUMA "; $data .= "nodes of this system, which will lead to poor performance. +"; $data .= "See),_NUMA_CRIT_,;(/proc/vmware/NUMA/hardware),_NUMA_CRIT_"; $data .= ",__REFERAL__file:__SELF__;( for details on your current "; $data .= "memory configuration),_NUMA_CRIT_,;"; (my $m_type = $data) =~ s/^((?:U|M))\|.*$/$1/; (my $p_list = $data) =~ s/^(?:U|M)\|(.*)$/$1/; my @sub_p_tmp = split(';',$p_list); my @args; for ( my $i = 0; $i <= $#sub_p_tmp; $i++ ) { if ( $sub_p_tmp[$i] =~ m/^\((.*)\),(.*),(.*)[,;]$/ ) { @args = ($1, $2, $3); } } #print "$m_type\n\n$p_list\n\n".Dumper(@sub_p_tmp)."\n"; print "\nArgs:\n"; print Dumper(@args)."\n";

The IF statement has a regex that should be working, returning output as follows:

Args: $VAR1 = 'Memory is incorrectly balanced between the NUMA nodes of this + system, which will lead to poor performance. See'; $VAR2 = '_NUMA_CRIT_';

However, Dumper doesn't return anything.


In reply to Matching a pattern which resides within round brackets by jbl_bomin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.