Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Removing some part of string

by Shaveta_Chawla (Acolyte)
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.

Comment on Removing some part of string
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 (Friar) 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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-09-22 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (182 votes), past polls