Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Removing some part of string

by Shaveta_Chawla (Sexton)
on Oct 18, 2011 at 12:25 UTC ( #932123=perlquestion: print w/replies, xml ) Need Help??
Shaveta_Chawla has asked for the wisdom of the Perl Monks concerning the following question:

I need to remove some part from a line. The line is

::
$line = U.S. abc xyz , newyork, USA, Feb. 2-4
$line =pqr ffg , Long Beach, california, Sep. 12-14
$line = Hello , St. Louis, Missouri, Mar. 29-31
$line = hello, How are u

I need to remove the last part from the string which contains month information using a general regular expression, which is suitable for all the lines and the line which doesn't have any month information should remain as it is.

Replies are listed 'Best First'.
Re: Removing some part of string
by Anonymous Monk on Oct 18, 2011 at 12:32 UTC
       while ($line){$_ =~ s/[ADFJMNOS][[:lower:]]{2}\.\s[0-3]{1,2}-\d{1,2}$//;}
Re: Removing some part of string
by zentara (Archbishop) on Oct 18, 2011 at 13:14 UTC
    Split on commas each line into an array and regex the $array-1 ( last element) for Jan Feb Mar Apr.... etc. Smells like homework.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Removing some part of string
by trizen (Hermit) on Nov 02, 2011 at 13:18 UTC
    while (defined($_ = <DATA>)) { my (@array) = split(/,/, $_, 0); if ($array[-1] =~ /^\h*(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct| +Nov|Dec)\./) { splice @array, -1; } print join(',', @array) . "\n"; } __DATA__ $line = U.S. abc xyz , newyork, USA, Feb. 2-4 $line =pqr ffg , Long Beach, california, Sep. 12-14 $line = Hello , St. Louis, Missouri, Mar. 29-31 $line = hello, How are u

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2016-12-02 23:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (51 votes). Check out past polls.