Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Parsing a Word placed between special characters

by kennethk (Monsignor)
on May 01, 2013 at 18:19 UTC ( #1031625=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Parsing a Word placed between special characters
Select or Download Code
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.

Re^2: Parsing a Word placed between special characters
by Laurent_R (Parson) 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+/;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1031625]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-10-20 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (92 votes), past polls