From: "David A. Black" Date: 2009-09-07T19:34:50+09:00 Subject: Re: regular expression NOT operator Hi -- On Mon, 7 Sep 2009, Phil Cooper-king wrote: > Regex is something I've managed to ignore for some time, only learning > bits when needed. > > I'm trying to do code highlighting, so I've been using Reg to find parts > of the code > I've run into an issue with comments > > # A comment can contain control statements like if and Constants > > things like if and a Constant get picked up by my reg, I'm trying to put > a not operator in my regex but cant seem to get it to work. > > so for constants I search /([A-Z].*?\b)/ > how can I add "when theres no # on the left"? > > I've been trying to use the ?! as the not operator. (is that even a not > operator) > this is what i've been trying /([A-Z].*?\b)(?!)/ There's negative look-behind in Oniguruma, but you're going to run into some difficulties anyway, I suspect. For example: x = 3 puts "This has no comments, and x is #{x}" if x < 5 My advice would be to keep it (relatively) straightforward by doing something like this as you scan the lines of text: comment_re = /^\s*#/ if comment_re.match(line) # treat line as a comment else # line is not a comment end David -- David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com Ruby/Rails training, mentoring, consulting, code-review Latest book: The Well-Grounded Rubyist (http://www.manning.com/black2) September Ruby training in NJ has been POSTPONED. Details to follow.