Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression

Status
Not open for further replies.

ssharmai

Programmer
Mar 16, 2012
1
US
Hello,
I am new to perl and trying to parse some reports. I am trying to get organism's name from the description.
Its working when field is like this:

ATP synthase F1, alpha subunit [Acidimicrobium ferrooxidans DSM 10331].
I am using this:
if($desc =~ /\[(.*?)\]/){$org = $1;}

But its not working when field is little complicated:
enoyl-[acyl-carrier-protein] reductase [NADH] 2 [Silicibacter sp. TrichCH4B].

I would really appreciate your help.

Thanks
Shalabh
 
Hi

And what would you like from there ?

To get the last matching part just [highlight #fcc]change [tt]if[/tt] to [tt]while[/tt][/highlight] and [highlight #cfc]add a [tt]g[/tt] flag[/highlight] :
Perl:
[highlight #fcc][b]while[/b][/highlight] [teal]([/teal][navy]$desc[/navy] [teal]=~[/teal] [green][i]/\[(.*?)\]/[/i][/green][highlight #cfc][b]g[/b][/highlight][teal])[/teal] [teal]{[/teal]
  [navy]$org[/navy] [teal]=[/teal] [navy]$1[/navy][teal];[/teal]
[teal]}[/teal]
But if you know that there are [highlight #ffc]no more words after that last part[/highlight], just [highlight #ccf]anchor the expression to the end[/highlight] and [highlight #fcf]forbid brackets[/highlight] ( [] ) :
Perl:
[b]if[/b] [teal]([/teal][navy]$desc[/navy] [teal]=~[/teal] [green][i]/\[([highlight #ccf][^\[\]][/highlight]*?)\][highlight #ffc]\W[/highlight][highlight #fcf]$[/highlight]/[/i][/green][teal])[/teal] [teal]{[/teal]
  [navy]$org[/navy] [teal]=[/teal] [navy]$1[/navy][teal];[/teal]
[teal]}[/teal]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top