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


in reply to Parsing a Word placed between special characters

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.

Replies are listed 'Best First'.
Re^2: Parsing a Word placed between special characters
by Laurent_R (Canon) on May 02, 2013 at 11:52 UTC

    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^2: Parsing a Word placed between special characters
by Anonymous Monk on May 02, 2013 at 09:40 UTC
    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.