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

Hi all, I have a xml document with multiple lines looking like:
text x="10.0".... text x="11.5".... text x="15.2".... text x="20.0"...
I want to use regex but how can i match those lines where x="?" is a multiple of 10 i.e. keep first and last line only? Thanks

2006-02-20 Retitled by GrandFather, as per Monastery guidelines
Original title: 'pattern match'

Replies are listed 'Best First'.
Re: pattern to match certain numbers only?
by holli (Abbot) on Feb 20, 2006 at 15:08 UTC
    You can't. Do something like the following:
    if ( /text x="(([0-9]+)\.([0-9]))"/ && $1 % 10 == 0 ) { #... }


    holli, /regexed monk/
      thanks for that - this will certainly help cheers
Re: pattern to match certain numbers only?
by tweetiepooh (Hermit) on Feb 20, 2006 at 15:45 UTC
    Check for the 0 as last digit prior to decimal
    if (/[1-9]0\.0/) { process the line }
    You'll need to expand this if you need to cope with hundreds and bigger. It also assumes there is a decimal point and a single digit after decimal.
      This might provide a little more flexibility, and a slightly tighter matching:
      if ( /=\"\d*?0\.\0+\"/ ) { # should match "0.0", "10.000", "120.0", etc... }
Re: pattern to match certain numbers only?
by CountOrlok (Friar) on Feb 20, 2006 at 15:10 UTC
    Use a module like XML::Simple to slurp the file into a hashref. Then loop through the hashref checking for the "x" value. If you don't know what the hashref will look like, use Data::Dumper to print it out so you can figure out how to loop through it.

    -imran