Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: regular expression

by NetWallah (Canon)
on Mar 16, 2012 at 15:32 UTC ( [id://959999]=note: print w/replies, xml ) Need Help??


in reply to regular expression

Assuming the "name" is the LAST entry inside square brackets, you can use something like this:
perl -e '(@x)=$ARGV[0]=~/\[(.*?)\]/g; print qq|$x[$#x];\n|' "enoyl-[a +cyl-carrier-protein] reductase [NADH] 2 [Silicibacter sp. TrichCH4B]. + " #--output--- Silicibacter sp. TrichCH4B;
Also, the regular expression is probably better written as:
/\[([^\]]+)\]/
(But that assumes matching "]" are guaranteed.)

             All great truths begin as blasphemies.
                   ― George Bernard Shaw, writer, Nobel laureate (1856-1950)

Replies are listed 'Best First'.
Re^2: regular expression
by muppetjones (Novice) on Mar 16, 2012 at 18:47 UTC

    As a side note, NetWallah's solution calls perl from the command line -- if you wanted to use it in your code, do this:

    while ($desc =~ /\[(.*?)\]/g) { } $org = $1 or '';
    or
    $desc =~ s/\[(.*?)\]//g; $org = $1 or '';

    The regex used essentially matches all data within brackets, but only saves the last one. The first example uses m//, and must therefore be run through a loop as the /g modifier merely saves the place where the previous regex match ended.

    The second option will destroy the string stored in $desc, but does not require the loop.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-18 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found