fujiman has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I encountered a parse problem and I'm hoping one of your experts can help me with.

I have a text string which I want to parse into 2 separate variables. The string is made up of 2 portions, <portion1><portion2>, where portion1 can be any texts but portion2 is one of the following Crit, Maj, or Okay.

#Sample strings in an array: my @a = ('SystemMaj', 'MemoryCrit','ServerOkay', 'MemoryOkay');

How would you write the code to accomplish this? I tried the following but failed.

#code sample my @a = ('SystemMaj', 'MemoryCrit','ServerOkay', 'MemoryOkay'); foreach my $v (@a) { $v =~ m/(\w+[^(?:Crit|?:Maj|?:Okay])(\w+)/; print "1: $1 2:$2\n"; }
#sample output 1: System 2:Maj #correct 1: Memo 2:ryCrit #should be 1: Memory 2: Crit 1: Serve 2:rOkay #should be 1: Server 2: Okay 1: Memo 2:ryOkay #should be 1: Memory 2: Okay

Thanks for your help.

fujiman

Replies are listed 'Best First'.
Re: Parse String Question
by karavelov (Monk) on Sep 19, 2008 at 00:41 UTC
    m/(\w+)(Crit|Maj|Okay)/
Re: Parse String Question
by jwkrahn (Abbot) on Sep 19, 2008 at 00:48 UTC
    $ perl -le' my @a = qw( SystemMaj MemoryCrit ServerOkay MemoryOkay ); foreach my $v (@a) { my @x = split /(Crit|Maj|Okay)/, $v; print "1: $x[0] 2: $x[1]"; } ' 1: System 2: Maj 1: Memory 2: Crit 1: Server 2: Okay 1: Memory 2: Okay

      Thank you both!