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

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

Replies are listed 'Best First'.
Re: Pattern with split
by imp (Priest) on Aug 07, 2006 at 14:35 UTC
    You could do this with split - but you shouldn't. I would recommend a capturing regex instead. Here's an example that satisfies your first match:
    my $pattern = qr/ (?: # Match beginning of line or whitespace sep +arator, no capture \A | \s+ ) ( # Capture one of: "[^"]*" # Matched double quotes | '[^']*' # Matched single quotes | {[^}]*} # Matched curly brackets | \S+? # non whitespace ) (?= # Match end of line or whitespace separator, + positive lookahead,no capture. \z | \s+ ) /x; my $data = '#VER "" 3 19950101 "Overforing BG till PG" 19950608'; my @values = $data =~ /$pattern/g; for my $i (0..$#values) { printf "%2d) %s\n",$i,$values[$i]; }
    Output:
    0) #VER 1) "" 2) 3 3) 19950101 4) "Overforing BG till PG" 5) 19950608
    For complex regular expressions like this I would recommend using the 'x' modifier as in the example above. It makes maintenance easier.
Re: Pattern with split
by davorg (Chancellor) on Aug 07, 2006 at 14:26 UTC

    This may not help at all (as you haven't actually told us what you're trying to do or what problems you are having) but have you looked at Text::ParseWords.

    Alternatively, if you have a specification for what all your potential input data looks like, then perhaps you can build a parser for it using Parse::RecDescent.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Pattern with split
by Fletch (Bishop) on Aug 07, 2006 at 14:27 UTC

    Here's an idea: don't. There's clever, and then there's trying to be too clever.

    Given that it looks like you've got several different record types which start with a different first field, the more maintainable approach might be to split or regex off the type field (my( $type, $rest ) = split( /\s+/, $record, 2 );) then use a dispatch table to figure out what to do with $rest.

Re: Pattern with split
by Hofmator (Curate) on Aug 07, 2006 at 14:22 UTC
    Well, this is speccing by example ... not-enough-information error :)

    But judging from the info you give us I would tend towards a capturing regex and not use split. Something like

    my ($type,$id,$no,$date1,$text,$date2)=/()()()()()()/;
    where the inside and outside of the parentheses are filled appropriately.

    -- Hofmator

Re: Pattern with split
by jkva (Chaplain) on Aug 07, 2006 at 14:17 UTC
    Ideas? I've got plenty of ideas... if they are good ones remains to be seen. You can trade them for one pony each ;-)
Re: Pattern with split
by ahmad (Hermit) on Aug 07, 2006 at 19:33 UTC

    first i think the examples in your question written in PHP , Right or Wrong ???? :Pp

    and if you could put data sample you want to split alone (with out the code) , it would be better .

    explain more what you want to do and you might get more help