Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Parsing a Word placed between special characters

by pillaipraveen (Initiate)
on May 01, 2013 at 18:11 UTC ( #1031624=perlquestion: print w/ replies, xml ) Need Help??
pillaipraveen has asked for the wisdom of the Perl Monks concerning the following question:

Thanks to everyone who responded. The query was in fact obscure since I didn't use the <code> tag. Please find the query in a proper format below.

I have a file called names.txt with the following content
john[JM]mcgroddy
stephen[SG]gomsey
yuri[YA]alchenko

I was trying to extract the initials placed between special characters  [ and ] and output this to a new file initials.txt as

JM
SG
YA

.

I hope that I have clarified the question. Thanks once again to those who guided me the correct regex. Regards, PS.

Comment on Parsing a Word placed between special characters
Select or Download Code
Re: Parsing a Word placed between special characters
by kennethk (Monsignor) on May 01, 2013 at 18:19 UTC

    Welcome to the Monastery. In the future, please wrap input in <code> tags so it doesn't get mangled. Note how chunks of your post got linkified.

    The basics of your question are documented in Extracting matches in perlretut. The only trick, assuming you mean to be using [ and ] as delimiters, is that these characters have special meaning in regular expressions, and thus must be escaped. Your regular expression might look something like /\[(.*?)\]/.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thanks to everyone who responded. The query was in fact obscure since I didn't use the <code> tag. Please find the query in a proper format below.

      I have a file called names.txt with the following content
      john[JM]mcgroddy
      stephen[SG]gomsey
      yuri[YA]alchenko

      I was trying to extract the initials placed between special characters  [ and ] and output this to a new file initials.txt as

      JM
      SG
      YA

      .

      I hope that I have clarified the question. Thanks once again to those who guided me the correct regex. Regards, PS.

      Again assuming the explored string is in the $_ special variable, the words between square brackets can be retrieved as follows:

      $name = $1 if /\w+\[(\w+)\]\w+/;
Re: Parsing a Word placed between special characters
by toolic (Chancellor) on May 01, 2013 at 18:22 UTC
Re: Parsing a Word placed between special characters
by davido (Archbishop) on May 01, 2013 at 18:37 UTC

    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

Re: Parsing a Word placed between special characters
by Laurent_R (Vicar) on May 01, 2013 at 18:52 UTC

    I guess the OP must have change the original post, since I do not see any square bracket in the original post right now.

    The line now look like this

    test\abc\test

    As it is now, I would suggest the following (assuming the explored string is in the $_ special variable):

    $name = $1 if /test\\(\w+)\\test/;

    But I am not sure that the description of the original string is really adequate.

      If someone posts \[abc\] without <code> tags, everyone will see \abc\. What's unclear to me is if the backslashes are significant or not.


      Dave

        I agree, the input is unclear.
Re: Parsing a Word placed between special characters
by hdb (Parson) on May 02, 2013 at 13:35 UTC

    Having the advantage of the well formatted question, I favor split over regex:

    use strict; use warnings; while(<DATA>){ print +( split /\]|\[/ )[1], "\n"; } __DATA__ john[JM]mcgroddy stephen[SG]gomsey yuri[YA]alchenko

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1031624]
Approved by toolic
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2014-07-13 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (244 votes), past polls