http://www.perlmonks.org?node_id=1031628


in reply to Parsing a Word placed between special characters

I couldn't tell from the formatting in your post whether your target strings look like abc\[def\]ghi, or if the backslashes were just your attempt to escape the bracket within the post. I'm going to assume that the backslash isn't intended to be part of the actual target strings, but if it is, you will just have to modify the regex by adding "\\" where appropriate.

my @strings = ( 'test[abc]test', 'test[cde]test', 'test[ack]test', ); my $regex = qr/ \b\[ # Require a word boundary and match an open bracket. (\w+) # Capture one or more "word" characters. \]\b # Match a close bracket and require a word boundary. /x; foreach my $string ( @strings ) { if( my( $name ) = $string =~ $regex ) { print "$name\n"; } }

Spend a few minutes with perlrequick and perlretut.


Dave