From: dblack@... Date: 2002-12-19T14:19:50+09:00 Subject: Re: How to include some text and exclude other in one regular expression? Hi -- On Thu, 19 Dec 2002, Greg Millam wrote: > On Thu, 19 Dec 2002, Usano wrote: > > > I would like to match "file 1 of 2" or "1 of 2", but not "day 1 of 2". > > > > I can match "file 1 of 2" with... > > > > /(file)? *\d+ *of *\d+/ > > > > I wonder if (in the same regex), I can prevent a match to "day 1 of > > 2"? The above expression will match this by just ignoring the word > > "day". > > > > Can this be done? > > /^(file)?\s*(\d+)\s*of\s*(\d+)$/ > > \s is any white space character. > adding ^ and $ to the beginning and end of the expression make it match > the entire string. since /1 or 2/ will match "day 1 or 2" "file 1 or 2" or > "In 2002, 1 or 2 people answered this email" > /^1 or 2$/ makes it match only "1 or 2", with nothing before or after. Not quite: irb(main):001:0> /^1 or 2$/.match("hi there\n1 or 2\nbye!") # ^ matches beginning of line and $ matches end of line. \A matches beginning of entire string, and \z matches end of string (or \Z, which is end of string minus terminating newline, if any). David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav