Thursday, 15 September 2011

Perl - Getting value from comma separated line -



Perl - Getting value from comma separated line -

i've got perl file parser trying rewrite. dynamic parser, , need extract value comma separated line.

the line want 1 value looks this:

entryname-8,44544,99955,52,156,15:16:16,15:19:16

(this line in each parsed file starts withentryname-. after - changes each file beingness parsed)

i want value after sec comma. (99955 in illustration above)

i have tried next without luck:

if (/ entryname-\((.*)\,(.*)\,(.*)\)/ ) { $entry_nr = $3; print "entry number = $entry_nr"; next; }

the problem first capture string .* greedy, consume of string. backtrack find 2 commas , result match end.

also:

you matching literal parentheses \( unusual reason. since not have such, never match. you not need escape commas \, you cannot have random space in regex / entry... unless have 1 in target string you not need capture strings not going use

a simple prepare utilize stricter capture grouping (including points above):

if (/entryname-\d+,\d+,(\d+)/ )

this capture $1.

as mpapec points out in comment, may wish utilize text::csv parse csv data. lot safer. if info simple enough, solution do.

perl

No comments:

Post a Comment