Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there!!!
How can I parse this string
name+C.+city+loc.+loc2+++++++++++++++++++++++++++++++++++++B+++v+G+++
using regular expression and get just this:
name+C.+city+loc.+loc2

My code:
$_=~ s/(w*\+{1}).*$/$1/g;
Is not working, any help, please??
Thnaks!!!!!

janitored by ybiC: Retitle from "Regular Exp.", minor formatting cleanup

Replies are listed 'Best First'.
Re: Parse string for fields
by snax (Hermit) on Oct 08, 2003 at 21:28 UTC
    Do you really want to lose the bits at the end?

    To smash the extra pluses, leaving all the other info intact, do this:

    tr/+/+/s;
    Otherwise it looks like shenme's got you covered below.
Re: Parse string for fields
by shenme (Priest) on Oct 08, 2003 at 21:28 UTC
    From what little you've said it would appear you want to get rid of everything after a bunch of '+'s appear.   How about just   s/\+{3,}.*$//;  .   We say to find the trailing part of the string that starts with at least three '+'s and throw away that and everything from there to the end of string.   If you want to throw away everything after even only two pluses, e.g. '++', you'd change the quantifier to {2,} or even change it to the more obvious   s/\+\+.*$//;  
Re: Parse string for fields
by NetWallah (Canon) on Oct 08, 2003 at 21:43 UTC
    Try this :
    my $b; my $a=q(name+C.+city+loc.+loc2++++++++++++++++++++++++++++++++ +++++B+++v+G+++); for(split /\+/ ,$a){ last if not $_; $b .='+' if $b; $b .= $_; #print qq([$_]\n) }; print $b; #--END--# ##Output:### name+C.+city+loc.+loc2
A reply falls below the community's threshold of quality. You may see it by logging in.