Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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:

Hello~

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.

Comment on Simple substitution
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://893899]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (18)
As of 2015-07-06 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (83 votes), past polls