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

hi,

i need a suggestion on how to do this in more elegant way

Identities\s+=\s+(.+)\s+\((.+)\%\),\s+Positives =\s+(.+)\s+\((.+)\%\), +\s+Negatives\s+=\s+(.+)\s+\((.+)\%\)/g
i have this regex that for which every line is evaluated through a while() loop. but some lines do not contain all necesery parameters for a regex to match and therefor if a parameter Positives is missing then the whole line would be skipped and that is not good. i know i could do it by splitting a line starting with a word Identities and then extract the data from it. but there are some complication that then appear and i wish to avoid that.

so my question is: is there a more elegant way to match a line check if parameters(in this case Identities,Negatives,Positives )exists and if some are missing just ignore that and deal with ones that do exist.

thnx

Update:

That is it dreadpiratepeter !!

thnx

Replies are listed 'Best First'.
Re: Regex question
by dreadpiratepeter (Priest) on Jul 21, 2009 at 13:12 UTC
    Assuming that there are no commas in your data other than those separating the params, I would split the lines on /\s*,\s*/, which would give you an array of your parameters, then loop through the parameters and match each of them. ie (warning, untested code)
    while (<>) { my @params = split /\s*,\s*/; foreach my $param (@params) { $param =~ /(Identities|Positives|Negatives)\s+=\s+(.+)\s+\((.+)\%\ +)/; # $1 now contains which type you matched, $2 and $3 are the submat +ches, do what you will with them } }
    or you could get fancier and do:
    while (<>) { my %params = map { my ($k,$v) = split /\s*=\s*/; $v =~ /(.+)\s+\((.+)\%\)/; ($k => [$1,$2]) } split /\s,\s*/; }
    and get a hash of all your params in one fell swoop.


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: Regex question
by JavaFan (Canon) on Jul 21, 2009 at 12:59 UTC
    i need a suggestion on how to do this in more elegant way
    i have this regex that for which every line is evaluated through a while() loop. but some lines do not contain all necesery parameters for a regex to match and therefor if a parameter Positives is missing then the whole line would be skipped and that is not good.
    Now I am confused. The first quote suggests you want another expression for your regexp. The second quote suggest that your regexp is wrong. Do you really want another wrong way to parse your lines?

    Personally, I'd go for correctness first, elegance later.

Re: Regex question
by CountZero (Bishop) on Jul 21, 2009 at 14:38 UTC
    Can you show some lines of the data to feed the regex and the results you wish to receive from the regex?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James