[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help with regular expression

toulax@gmail.com

6/28/2007 6:54:00 PM

How can I make a regular expression that will match everything, unless
it contains a certain string then it will match nothing.

For instance, let's say it should not match "bar", then:

"foo" => "foo"
"bar " => nil
"foobar" => nil

Thanks in advance

15 Answers

Philip Hallstrom

6/28/2007 7:00:00 PM

0

toulax@gmail.com

6/28/2007 7:07:00 PM

0

On Jun 28, 4:00 pm, Philip Hallstrom <r...@philip.pjkh.com> wrote:
> > How can I make a regular expression that will match everything, unless
> > it contains a certain string then it will match nothing.
>
> > For instance, let's say it should not match "bar", then:
>
> > "foo" => "foo"
> > "bar " => nil
> > "foobar" => nil
>
> some_string !~ /bar/
>
> -philip

Yeah, that's the easy way, is it possible to do that with the regular
expression alone and not use !~? The reason is because this is not
exactly for Ruby and I don't have another option.

Thanks

Axel Etzold

6/28/2007 7:50:00 PM

0

I think what you want is what's called negative lookahead.
I don't think it's available in Ruby's Regexps anyway,
and resorting to Oniguruma isn't an option as you don't
want to use Ruby anyway. Maybe you just break it down into
more steps as here:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

Or you look for negative lookahead here:

http://www.regular-expressions.info/looka...

(provided the language you'll eventually use sports this feature
at all).

Best regards,

Axel
--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/mult...

Jesse Hk

6/28/2007 9:35:00 PM

0

Am I missing something here? Why not just test for equality?


irb(main):001:0> "foo" == "foo"
=> true
irb(main):002:0> "foo" == "bar"
=> false

--
Posted via http://www.ruby-....

dblack

6/28/2007 9:41:00 PM

0

yermej

6/28/2007 9:44:00 PM

0

On Jun 28, 2:49 pm, "Axel Etzold" <AEtz...@gmx.de> wrote:
> I think what you want is what's called negative lookahead.
> I don't think it's available in Ruby's Regexps anyway,
> and resorting to Oniguruma isn't an option as you don't
> want to use Ruby anyway. Maybe you just break it down into
> more steps as here:
>
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
>
> Or you look for negative lookahead here:
>
> http://www.regular-expressions.info/looka...
>
> (provided the language you'll eventually use sports this feature
> at all).
>
> Best regards,
>
> Axel

Positive and negative lookahead work in Ruby Regexp. I guess that
doesn't help the original poster, but FYI and all.

Jeremy

Stephen Ball

6/29/2007 2:25:00 AM

0

On 6/28/07, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
> >
> On Fri, 29 Jun 2007, Jesse Hk wrote:
>
> > Am I missing something here? Why not just test for equality?
> >
> >
> > irb(main):001:0> "foo" == "foo"
> > => true
> > irb(main):002:0> "foo" == "bar"
> > => false
>
> That doesn't cover the case where the string includes the substring
> "bar" but also includes other characters.
>
>
> David
>

In that case it still works, so we have:

"foo" => "foo"

irb(main):001:0> "foo" == "foo"
=> true

"bar " => nil

irb(main):002:0> "foo" == "bar"
=> false

"foobar" => nil

irb(main):003:0> "foo" == "foobar"
=> false

-- Stephen

toulax@gmail.com

6/29/2007 3:28:00 AM

0

Like I said it has to be a regular expression, nothing else, any
ideas?

On Jun 28, 11:24 pm, "Stephen Ball" <sdb...@gmail.com> wrote:
> On 6/28/07, dbl...@wobblini.net <dbl...@wobblini.net> wrote:
>
>
>
> > Hi --
>
> > On Fri, 29 Jun 2007, Jesse Hk wrote:
>
> > > Am I missing something here? Why not just test for equality?
>
> > > irb(main):001:0> "foo" == "foo"
> > > => true
> > > irb(main):002:0> "foo" == "bar"
> > > => false
>
> > That doesn't cover the case where the string includes the substring
> > "bar" but also includes other characters.
>
> > David
>
> In that case it still works, so we have:
>
> "foo" => "foo"
>
> irb(main):001:0> "foo" == "foo"
> => true
>
> "bar " => nil
>
> irb(main):002:0> "foo" == "bar"
> => false
>
> "foobar" => nil
>
> irb(main):003:0> "foo" == "foobar"
> => false
>
> -- Stephen


yermej

6/29/2007 4:12:00 AM

0

On Jun 28, 10:27 pm, "tou...@gmail.com" <tou...@gmail.com> wrote:
> Like I said it has to be a regular expression, nothing else, any
> ideas?
>
> On Jun 28, 11:24 pm, "Stephen Ball" <sdb...@gmail.com> wrote:
>
> > On 6/28/07, dbl...@wobblini.net <dbl...@wobblini.net> wrote:
>
> > > Hi --
>
> > > On Fri, 29 Jun 2007, Jesse Hk wrote:
>
> > > > Am I missing something here? Why not just test for equality?
>
> > > > irb(main):001:0> "foo" == "foo"
> > > > => true
> > > > irb(main):002:0> "foo" == "bar"
> > > > => false
>
> > > That doesn't cover the case where the string includes the substring
> > > "bar" but also includes other characters.
>
> > > David
>
> > In that case it still works, so we have:
>
> > "foo" => "foo"
>
> > irb(main):001:0> "foo" == "foo"
> > => true
>
> > "bar " => nil
>
> > irb(main):002:0> "foo" == "bar"
> > => false
>
> > "foobar" => nil
>
> > irb(main):003:0> "foo" == "foobar"
> > => false
>
> > -- Stephen

As Axel said, you need to use a regex with negative lookahead. This
should work:
/b(?!ar)/
as long as whichever language/system you're using supports it. Maybe
there's a better group or forum for your question if you're not using
Ruby.

I would have thought this would work:
/.*(?!bar)/
but it doesn't in Ruby. I would take that to mean 0 or more of any
character not followed by bar, but I guess that's not the case. I
(obviously) don't understand negative lookahead all that well. Can
anybody explain?

Jeremy

toulax@gmail.com

6/29/2007 4:26:00 AM

0

On Jun 29, 1:11 am, "yer...@gmail.com" <yer...@gmail.com> wrote:
> On Jun 28, 10:27 pm, "tou...@gmail.com" <tou...@gmail.com> wrote:
>
>
>
> > Like I said it has to be a regular expression, nothing else, any
> > ideas?
>
> > On Jun 28, 11:24 pm, "Stephen Ball" <sdb...@gmail.com> wrote:
>
> > > On 6/28/07, dbl...@wobblini.net <dbl...@wobblini.net> wrote:
>
> > > > Hi --
>
> > > > On Fri, 29 Jun 2007, Jesse Hk wrote:
>
> > > > > Am I missing something here? Why not just test for equality?
>
> > > > > irb(main):001:0> "foo" == "foo"
> > > > > => true
> > > > > irb(main):002:0> "foo" == "bar"
> > > > > => false
>
> > > > That doesn't cover the case where the string includes the substring
> > > > "bar" but also includes other characters.
>
> > > > David
>
> > > In that case it still works, so we have:
>
> > > "foo" => "foo"
>
> > > irb(main):001:0> "foo" == "foo"
> > > => true
>
> > > "bar " => nil
>
> > > irb(main):002:0> "foo" == "bar"
> > > => false
>
> > > "foobar" => nil
>
> > > irb(main):003:0> "foo" == "foobar"
> > > => false
>
> > > -- Stephen
>
> As Axel said, you need to use a regex with negative lookahead. This
> should work:
> /b(?!ar)/
> as long as whichever language/system you're using supports it. Maybe
> there's a better group or forum for your question if you're not using
> Ruby.
>
> I would have thought this would work:
> /.*(?!bar)/
> but it doesn't in Ruby. I would take that to mean 0 or more of any
> character not followed by bar, but I guess that's not the case. I
> (obviously) don't understand negative lookahead all that well. Can
> anybody explain?
>
> Jeremy

Thanks Jeremy, as you said this doesn't seem to work, but thanks for
pointing me to the right track. Also if anyone can suggest a group
more specific to regexp I'm all ears.