On Sat, Mar 7, 2009 at 11:53, Rick <rich.j...@gmail.com> wrote: > __test-code__ > use warnings; > use strict; > my $test_s = 'hi [how are you]'; > > my $data; > if ( $test_s =~ m/\[.+\]/ ) { > $data = $1; > } > __END__ > > is there easier way to do this > I was hoping something like this would work > > $data = ($test_s =~ m/\[.+\]/); but does not work.. I tried @data too but > still don't work.. > I want to assign what was matched directly into the variable without going > through if statement.
A regex returns all captures found when it is in list context, so if you add a capture to your regex and then make certain the call is in list context (instead of scalar context) you should get what you want: my ($data) = $test_s =~ /(\[(.+)\]/; The code above assumes you want what is between the first and last brackets in $test_s. If the string contains more than one set of brackets you may want to say this instead: my ($data) = $test_s =~ /(\[(.+?)\]/; -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/