Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Simple substitution

by eversuhoshin (Sexton)
on Mar 18, 2011 at 03:36 UTC ( #893899=perlquestion: print w/replies, xml ) Need Help??
eversuhoshin has asked for the wisdom of the Perl Monks concerning the following question:


I need help with simple substitution. I have financial report files that are in format of "name_year." e.g. (GE_2002).

Unfortunately, somewhere miscoded such as Goldman_Sachs_2002.

Can anyone help me write a substitution code that only keeps the last underscore "_"?

So Goldman_Sachs_2002 becomes GoldmanSachs_2002.

Thank you so much for your time and consideration.

Replies are listed 'Best First'.
Re: Simple substitution
by BrowserUk (Pope) on Mar 18, 2011 at 03:39 UTC

    Use a lookahead:

    $s = 'Goldman_Sachs_2002';; $s =~ s[_(?=.*_)][]g;; print $s;; GoldmanSachs_2002

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Simple substitution
by wind (Priest) on Mar 18, 2011 at 04:10 UTC
    Negative lookahead assertion to avoid last _.
    my $str = 'Goldman_Sachs_2002'; $str =~ s/_(?!\d*$)//g; print $str;
      Thank you so much :) I am reading perl regular expression but I am still a novice. Thank you again!!!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://893899]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2018-05-22 18:28 GMT
Find Nodes?
    Voting Booth?