From: Robert Klemme Date: 2006-03-10T20:38:43+09:00 Subject: Re: Small regexp question francisrammeloo@hotmail.com wrote: > Hi all, > > I am writing some refactoring code for a C++ project. > > I need to change: > > class MyClass > { > ... > } > > to: > > class IMP_EXP MyClass > { > ... > } > > The pattern I used to find a class definition line is: > > line =~ /^\s*class\s+(\w+)/ > > But I want to exclude forward class declarations ( class MyClass; ) > > So I changed my pattern to: > > line =~ /^\s*class\s+(\w+)\s*[^;]/ --> don't match if line ends > with ";" > > But it doesn't work... Why? Because the match simply stops before the ";". >> line = 'class Foo;' => "class Foo;" >> line[/^\s*class\s+(\w+)\s*[^;]/] => "class Foo" If you want to make sure there is no ";" between the class name and the end of the line you need to anchor the RX at the end: >> line = 'class Foo;' => "class Foo;" >> line[/^\s*class\s+(\w+)[^;]*$/] => nil >> line = 'class Foo' => "class Foo" >> line[/^\s*class\s+(\w+)[^;]*$/] => "class Foo" Kind regards robert