From: Jason Sweat Date: 2005-03-24T11:09:15+09:00 Subject: Re: (Maybe) a simple question about regex On Thu, 24 Mar 2005 10:49:49 +0900, Sam Kong wrote: > Hello! > > I think that I am missing a very simple concept about regex. > > s = '0123456789' > s.scan(/\d\d/) #-> ["01", "23", "45", "67", "89"] > > Now I want to exclude "45". > How can I express it in the regex? > When it's only one character, I can use ^. > But for 2 characters, I don't think I can use it. > > What I want is: > > s = '0123456789' > s.scan(some_regex) #-> ["01", "23", "67", "89"] > > What should some_regex be? You can use a negative assertion to say you want to skip "45", but it will bump forward one space and you will end up with the last matches being "56" and "78" >> s.scan(/(?!45)\d\d/) => ["01", "23", "56", "78"] So with a little uglier assertion, you can say: >> s.scan(/(?!45|5)\d\d/) => ["01", "23", "67", "89"] and get what you specified, but though it works for your toy case, I would be worried that this might not extrapolate out to your real goal well. HTH Regards, Jason http://blog.casey-sweat.us/