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

I'm very poor on regex and have a problem to solve. I have something like the following being read from a file
SMSG(could be anything here.) SMSG(And more then one occurrence. And the following @ symbol is sometimes missing) @ JOB2(AGE=42, NAME=Robert, DATE=@TODAY)
What I need to extract is the contents of the last set of brackets as a string and the word immediately before the open bracket into a seperate string, discarding the rest. Can anyone help?
Thanks in advance.

Replies are listed 'Best First'.
Re: The last occurrence
by ccn (Vicar) on Jul 20, 2004 at 12:47 UTC
    $sting =~ m{\b # word boundary ( \S+ # word before '(' \s* # spaces if any \( [^\)]+ # '(' inside of ')' \) ) [^\)]* # some not ')' chars if any $ # end of string ancor }x; $result = $1;

    Upd: comments added

      Thank you for your help this worked but gave me the whole thing in one string but it only took a momment to get what I needed from this, you have saved me a lot of time
      thanks again
Re: The last occurrence
by Dietz (Curate) on Jul 20, 2004 at 13:05 UTC
    #!/usr/bin/perl -w use strict; { local $/ = ""; while(<DATA>) { my($word, $string) = $_ =~ /.*(\b\w+)\s*(\(.*\))/s; print "\$word: $word\n"; print "\$string: $string\n"; } } __DATA__ SMSG(could be anything here.) SMSG(And more then one occurrence. And the following @ symbol is sometimes missing) @ JOB2 (AGE=42, NAME=Robert, DATE=@TODAY)