jbl_bomin has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|