in reply to Parse string for fields

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: (2) Parse string for fields
by davido (Cardinal) on Oct 08, 2003 at 23:39 UTC
    my $string = "name+C.+city+loc.+loc2+++++++++++++++++++++++++B+++v+G++ ++"; $string =~ s/\+(?!\+)|\+{3,}.*$/ /g; print $string, "\n";
    That will work though it leaves a trailing space.
    Why not use split?

    my @fields = split /\+/, $string, 6;
    Now @fields[0..4] contain what you want, and the garbage ("BvG") is in $fields[5].

    Take it one step further like this:

    my $parsed = join " ",(split /\+/, $string, 6)[0..4];

    Do it the hard way with a substitution regexp, or the easy way with split. :)


    Dave


    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein
Re: (2) Parse string for fields
by snax (Hermit) on Oct 08, 2003 at 23:21 UTC
    Look at this node above: Re: Parse string for fields (courtesy of NetWallah) -- that will give you what you want by changing *one* character. I leave it to you to find the right character :)

    Update: Edited to account for node rearrangements.