Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Parsing a Word placed between special characters

by davido (Archbishop)
on May 01, 2013 at 18:37 UTC ( #1031628=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1031628]
help
Chatterbox?
[marto]: well, let me know in advance, I'll buy you a pint :)
[Corion]: robby_dobby: No, I'm returning in the second workweek of January. The main hectic parts are in the first days after the start of the new year
[robby_dobby]: marto: and, I'm not sure I'd be around here long enough for YAPC::EU
[marto]: Corion nice, what are you plans for Christmas?
[robby_dobby]: Corion: Nice, you just won't be around to deal with the mess - enjoy your vacation :-)
[robby_dobby]: marto: Sure, will do. Thanks!
[Corion]: marto: No great plans - I'll meet with my sister, my brother and my mother, but that's all :)
[Corion]: ... and also a visit to my godson+family of course :)
Corion orders some gifts online :)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2017-12-15 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (431 votes). Check out past polls.

    Notices?