Negation of regular expression


(Yasuo Saito) #1

Hi.

If any regular expression r is given,
how can I write the negation of r in Ruby?

I want something like operator ! in Perl – eg. grep(!/foo/, @bar) --,
or option -v of egrep – eg. egrep -v foo file --.

···


Y.Saito


(Ned Konz) #2

@bar.reject { |ea| ea ~ /foo/ }

···

On Friday 28 June 2002 06:55 am, Yasuo Saito wrote:

Hi.

If any regular expression r is given,
how can I write the negation of r in Ruby?

I want something like operator ! in Perl – eg. grep(!/foo/, @bar)
–, or option -v of egrep – eg. egrep -v foo file --.


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE


(Albert) #3

irb(main):043:0> x = ‘abcd’
"abcd"
irb(main):044:0> x =~ /bc/
1
irb(main):045:0> x =~ /ef/
nil
irb(main):046:0> x !~ /bc/
false
irb(main):047:0> x !~ /ef/
true

···

On Friday 28 June 2002 08:55 am, Yasuo Saito wrote:

Hi.

If any regular expression r is given,
how can I write the negation of r in Ruby?

I want something like operator ! in Perl – eg. grep(!/foo/, @bar) --,
or option -v of egrep – eg. egrep -v foo file --.


(Yasuo Saito) #4

If any regular expression r is given,
how can I write the negation of r in Ruby?

I want something like operator ! in Perl – eg. grep(!/foo/, @bar)
–, or option -v of egrep – eg. egrep -v foo file --.

@bar.reject { |ea| ea ~ /foo/ }

Thank you for your answer.
But, how should I do in the following case?

class A
def initialize(string)
@string = string
end
def execute(regexp)
if (@string =~ regexp) then
# …
else
# …
end
end
end

class B
def execute(a, regexp)
a.execute(!regexp) # ERROR… How should I do?
end
end

B.new.execute(A.new(“foo”), /bar/)

···


Y.Saito


(Ned Konz) #5

class A
def initialize(string)
@string = string
end
def execute(regexp, truth=true)
if (@string =~ regexp) == truth then
# …
else
# …
end
end
end

class B
def execute(a, regexp)
a.execute(regexp, false) # ERROR… How should I do?
end
end

B.new.execute(A.new(“foo”), /bar/)

···

On Friday 28 June 2002 07:33 am, Yasuo Saito wrote:

But, how should I do in the following case?

class A
def initialize(string)
@string = string
end
def execute(regexp)
if (@string =~ regexp) then
# …
else
# …
end
end
end

class B
def execute(a, regexp)
a.execute(!regexp) # ERROR… How should I do?
end
end

B.new.execute(A.new(“foo”), /bar/)


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE


(Yasuo Saito) #6

class A
def initialize(string)
@string = string
end
def execute(regexp, truth=true)
if (@string =~ regexp) == truth then
# …
else
# …
end
end
end

class B
def execute(a, regexp)
a.execute(regexp, false) # ERROR… How should I do?
end
end

B.new.execute(A.new(“foo”), /bar/)

Thanks again.
But, I don’t want to use a `flag’, if possible.
So, I’ll change my question a little.

If a certain regular expression is given,
can the negation of the regular expression also
be expressed by a regular expression?

···


Y.Saito