Help with regular expression

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

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

How about a negative look-ahead like the one used here
Regex: except when - Ruby - Ruby-Forum ?

If the string contains "bar", the regex matches the rest of the string
that does contain no further "bar" substring, i.e. /ar.*$/.

ruby -e 'r = /(?!.*bar).*/; puts r.match("foobarxxxfoobarxxx"); puts
r.match("foorab"); puts r.match("yyybarbar")'

=> arxxx
=> foorab
=> ar

Cheers

verno

···

toulax@gmail.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

Thanks in advance

--
Posted via http://www.ruby-forum.com/\.

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

···

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

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-talk/131694

Or you look for negative lookahead here:

http://www.regular-expressions.info/lookaround.html

(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/multimessenger

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-forum.com/.

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

Jeremy

···

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-talk/131694

Or you look for negative lookahead here:

Regex Tutorial - Lookahead and Lookbehind Zero-Length Assertions

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

Best regards,

Axel

Hi --

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

···

On Fri, 29 Jun 2007, Jesse Hk wrote:

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

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

···

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

some_string unless some_string.match("bar")

···

On 6/28/07, Stephen Ball <sdball@gmail.com> wrote:

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

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

Hi --

···

On Fri, 29 Jun 2007, Stephen Ball wrote:

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

True; I got it backwards. What it really doesn't cover is the case of
a string that doesn't contain "bar" but isn't equal to "foo":

"foo" == "abcdef" # false, but should be true because "abcdef"
                      # does not include "bar"

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

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

···

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

Please disclose more detail on the environment you are using and what exactly you want to do. Otherwise it's difficult to come up with suggestions. If it is a different programming language it might also be a good idea to post to a specific forum for that language.

Kind regards

  robert

···

On 29.06.2007 05:27, toulax@gmail.com wrote:

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

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.

···

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

Sorry, but I was wrong. I /b(?!ar)/ isn't right because it would only
match a string if it contains a 'b'. I would have thought /.*(?!bar)/,
but as I said, that doesn't work in Ruby and probably shouldn't now
that I've thought about it more.

Probably just ignore everything I've said except for the fact that
you'll need negative lookahead. I think. Oh, and the part about asking
in a more appropriate group if you aren't really looking for a Ruby
regex.

Jeremy

···

On Jun 28, 11:11 pm, "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.

One additional source for Regexp in general and in depth is the book by Jeffrey Friedl,"Mastering Regular Expressions", published at O'Reilly.
I have to admit that I couldn't solve the problem you were mentioning
in some reasonable time -- and didn't really get a concise clue from
cross-reading that book either - even though it's considered the encyclopedia
of the subject.
I don't really understand why you can't do the search in two steps:

1. search for everything that contains bar as a substring and
2 . eliminate it from all results ...?

Best regards,

Axel

···

--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: Handytarif Vergleich 2023 | alle Anbieter vergleichen | GMX