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

Hi monks. I am sure this is an easy task. But I am having hard time learning regular expression. I am working on a project which contains file that has bunch of numbers at the end of each line. I need to match these numbers. I appreciate your help.
54 55 20 21 22 25 27 35 36 37 38 40 86 99 113 115 158 161 165 166 216 220 231 232 233 234 235 236 237 255 256 257 258 259 260 261 262 278 279 280 281 282 283 284 285 286 287 288 289 290 291 294

Replies are listed 'Best First'.
Re: Why regular expression so hard?!
by davorg (Chancellor) on Aug 31, 2000 at 19:55 UTC

    Building a regex that matches only that set of numbers isn't particularly hard, but it would be a huge regex and not very efficient. I'd consider doing it without a regex. Something like this perhaps:

    my @list = qw(54 55 20 21 22 25) # simplifed greatly :) my %hash; @hash{@list} = (1) x @list; while (<FILE>) { # assuming FILE has been opened previously my @row = split(/,/); # assuming comma separator if ($hash{$row[-1]}) { # process line } }

    p.s. Oh, and regexen are hard because they're so powerful :)

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

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
Re: Why regular expression so hard?!
by gnat (Beadle) on Aug 31, 2000 at 23:19 UTC
    Regular expressions are hard for two reasons:
    1. They are capable of quite powerful things. Baby talk is simple, but any language used for real tasks is quite complex.
    2. They are often badly taught or explained.
    That answers the question in your subject. The others solved your problem already, so I'll stay silent on that :-)

    Nat

Re: Why regular expression so hard?!
by merlyn (Sage) on Aug 31, 2000 at 19:51 UTC
      I am sorry. May be I should rephrase my question. These are not the only numbers in file. There are more numbers with each line. I just need to match these one and print. Thanks.
        perl -ne 'if (/(\d+(\.\d+)?)$/) {print $1, "\n"}' file1 file2
        That picks up numbers at the end of the line. If you want the last number in the line (ignoring possible trailing text) throw in \D* before the $.
        If you are looking to only match the numbers on the end of each line use the regex /(\d+)$/. The $ will anchor it to the end of the line for you... So getting all the numbers at the ends of the lines would be
        my @numbers; while ( <FILE> ) { /(\d+)$/; push @numbers, $1; }
        Update: oops... As merlyn points out, don't do this.
        So how do you decide whether a number is to be printed or not? Can you give a rule? Is it based on what it is, or where it is, or where it isn't?

        -- Randal L. Schwartz, Perl hacker

Re: Why regular expression so hard?!
by Anonymous Monk on Aug 31, 2000 at 20:05 UTC
    Answering Professor Merlyn's question, It is based on what it is . I can do the rest. Thanks.
Re: Why regular expression so hard?!
by vrempire (Sexton) on Sep 07, 2000 at 21:51 UTC
    Why regular expression is hard? 1.regular expression is the beauty of the perl. 2.you can impress your friends if you can write one obsfuscated regex. 3.if you can do one regex that works,take it as your masterpiece. 4.it is thrilling and chilling for the beginners,but exciting and tempting if you started on it. Anybody wanna add to the list? :> -vrempire-