[thelist] RegExp: extracting a substring

Hassan Schroeder hassan at webtuitive.com
Thu Sep 11 15:47:46 CDT 2003


Tab Alleman wrote:

> Ok, so I've got my regexp pattern:
> 
> "^PER NIGHT STARTING [0-9]?[0-9]\b?[A-Z][A-Z][A-Z] FOR (\d{1,2})
> NIGHTS?$"
> 
> And I want to get the value of (\d{1,2}) when I find a match.  In other
> words, "how many nights?"  The lengths of the delimiters can vary, so I
> don't see how I can use Mid() to parse it out.  I thought RegExp's were
> supposed to be able to get just what is in the parenthesis, but
> apparently that's not the case.  What's the point of putting things in
> parenthesis anyway?

To get just what is in the parenthesis :-)

> Anyway, anybody have any idea how I can get the value of (\d{1,2}) for
> each match?

$1 -- in Perl, at least:

#!perl

while(<>){
   if ( $_ =~ /PER NIGHT STARTING \d{1,2}\s?[A-Z]{3} FOR (\d{1,2}) NIGHT/ )
   {
     print $1 . "\n";
   }
}

Note that I did slightly mod your original regexp...

HTH!
-- 
Hassan Schroeder ----------------------------- hassan at webtuitive.com
Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com

                           dream.  code.





More information about the thelist mailing list