Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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 wandering the Monastery: (9)
As of 2014-10-24 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls