Reg Exp

Can somebody hep me with a very simple regular expression. I want to
write an regex which if matches a specific string should return false.
for example:
if i have string "Hello world", and i want to say, if you find "hello"
in this string then return false. Can someone explain how to write this?

···

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

Simple approach:

irb(main):007:0> a = "hello world"
=> "hello world"
irb(main):008:0> !(a =~ /hello/)
=> false
irb(main):009:0> !(a =~ /asdfsdf/)
=> true

Jesus.

···

On Tue, Apr 15, 2008 at 2:59 PM, Dipesh Batheja <dipesh_batheja@yahoo.com> wrote:

Can somebody hep me with a very simple regular expression. I want to
write an regex which if matches a specific string should return false.
for example:
if i have string "Hello world", and i want to say, if you find "hello"
in this string then return false. Can someone explain how to write this?

I'm not sure what you're going to gain from a regex if it's a specific
string and not a pattern.

irb(main):001:0> a = "Hello World"
=> "Hello World"
irb(main):002:0> !(a.include? 'Hello')
=> false
irb(main):003:0>

It seems you'd want to wrap that into a method to make the intent a little
more obvious

irb(main):011:0> def should_filter?(source, word)
irb(main):012:1> source.include? word
irb(main):013:1> end
=> nil
irb(main):014:0> should_filter?(a, 'Hello')

Just a thought.

to make the regex work as in your example though, you'd do this:
irb(main):021:0> !(a =~ /hello/i)
=> false

(case insensitive matching)

···

On Tue, Apr 15, 2008 at 8:59 AM, Dipesh Batheja <dipesh_batheja@yahoo.com> wrote:

Can somebody hep me with a very simple regular expression. I want to
write an regex which if matches a specific string should return false.
for example:
if i have string "Hello world", and i want to say, if you find "hello"
in this string then return false. Can someone explain how to write this?
--
Posted via http://www.ruby-forum.com/\.

--
Jeff Schoolcraft
http://thequeue.net/blog/
Microsoft MVP

use the TextualRegexp gem

-------------------------------------------------------|
~ Ari
Careful - I'm like Stallman with katanas!

···

On Apr 15, 2008, at 8:59 AM, Dipesh Batheja wrote:

Can somebody hep me with a very simple regular expression. I want to
write an regex which if matches a specific string should return false.
for example:
if i have string "Hello world", and i want to say, if you find "hello"
in this string then return false. Can someone explain how to write this?

Problem is i have function, which takes a regex as the parameter. I want
to pass this regex in such a way that the function returns false if the
string is matched and true if it isn't. I want the regex itself specify
that if it matches the given string then it is false otherwise true.

Jesús Gabriel y Galán wrote:

···

On Tue, Apr 15, 2008 at 2:59 PM, Dipesh Batheja > <dipesh_batheja@yahoo.com> wrote:

Can somebody hep me with a very simple regular expression. I want to
write an regex which if matches a specific string should return false.
for example:
if i have string "Hello world", and i want to say, if you find "hello"
in this string then return false. Can someone explain how to write this?

Simple approach:

irb(main):007:0> a = "hello world"
=> "hello world"
irb(main):008:0> !(a =~ /hello/)
=> false
irb(main):009:0> !(a =~ /asdfsdf/)
=> true

Jesus.

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

Ok let me try explain my situation a little bit.
I have default route in ROR environment.rb:
map.connect ':lang/:canonic/:controller/:action/:id

it matches paths like:
http://www.abc.com/en/acme/products/show/1

where :canonic is "acme" in this example

I have a specific case where :canonic cannot be "admin"

map.connect method take a key :requirements where I can specify a regex:
map.connect ':lang/:canonic/:controller/:action/:id, :requirement =>
{:canonic => /regex/ }

I need to write in the reg ex, in such a way, so that when something
like:

http://www.abc.com/en/admin/products/show/1

comes, it doesn't match the path.

I hope this helps in explaining my situation.

Jeff Schoolcraft wrote:

···

I'm not sure what you're going to gain from a regex if it's a specific
string and not a pattern.

irb(main):001:0> a = "Hello World"
=> "Hello World"
irb(main):002:0> !(a.include? 'Hello')
=> false
irb(main):003:0>

It seems you'd want to wrap that into a method to make the intent a
little
more obvious

irb(main):011:0> def should_filter?(source, word)
irb(main):012:1> source.include? word
irb(main):013:1> end
=> nil
irb(main):014:0> should_filter?(a, 'Hello')

Just a thought.

to make the regex work as in your example though, you'd do this:
irb(main):021:0> !(a =~ /hello/i)
=> false

(case insensitive matching)

On Tue, Apr 15, 2008 at 8:59 AM, Dipesh Batheja
<dipesh_batheja@yahoo.com>

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

I don't think you'll be able to do what you want with just a regex pattern.
The closest you could get is negative lookahead. You'd have to pull out the
first character and use negative lookahead for the rest, it will probably
only work for simple strings.

irb(main):029:0> a = "Hello World"
=> "Hello World"
irb(main):030:0> a =~ /h(?!ello)/i
=> nil
irb(main):031:0> b = "Hel l o world"
=> "Hel l o world"
irb(main):032:0> b =~ /h(?!ello)/i
=> 0

···

On Tue, Apr 15, 2008 at 9:17 AM, Dipesh Batheja <dipesh_batheja@yahoo.com> wrote:

Problem is i have function, which takes a regex as the parameter. I want
to pass this regex in such a way that the function returns false if the
string is matched and true if it isn't. I want the regex itself specify
that if it matches the given string then it is false otherwise true.

Jesús Gabriel y Galán wrote:
> On Tue, Apr 15, 2008 at 2:59 PM, Dipesh Batheja > > <dipesh_batheja@yahoo.com> wrote:
>> Can somebody hep me with a very simple regular expression. I want to
>> write an regex which if matches a specific string should return false.
>> for example:
>> if i have string "Hello world", and i want to say, if you find "hello"
>> in this string then return false. Can someone explain how to write
this?
>
> Simple approach:
>
> irb(main):007:0> a = "hello world"
> => "hello world"
> irb(main):008:0> !(a =~ /hello/)
> => false
> irb(main):009:0> !(a =~ /asdfsdf/)
> => true
>
> Jesus.

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

--
Jeff Schoolcraft
http://thequeue.net/blog/
Microsoft MVP

[mailto:list-bounce@example.com] On Behalf Of Dipesh Batheja
# map.connect ':lang/:canonic/:controller/:action/:id
# it matches paths like:
# http://www.abc.com/en/acme/products/show/1
# where :canonic is "acme" in this example
# I have a specific case where :canonic cannot be "admin"
# map.connect method take a key :requirements where I can
# specify a regex:
# map.connect ':lang/:canonic/:controller/:action/:id, :requirement =>
# {:canonic => /regex/ }
# I need to write in the reg ex, in such a way, so that when something
# like:
# http://www.abc.com/en/admin/products/show/1
# comes, it doesn't match the path.

try the ?!, this seems to negate the regex
eg, the following will strictly not allow admin and case-combi like Admin. it will thus allow xadmin, radmin, administration, etc..

irb(main):044:0> /(?!^admin$).{5,5}/i =~ "admin"
=> nil
irb(main):045:0> /(?!^admin$).{5,5}/i =~ "Admin"
=> nil
irb(main):046:0> /(?!^admin$).{5,5}/i =~ "ADMIN"
=> nil
irb(main):047:0> /(?!^admin$).{5,5}/i =~ "administration"
=> 0
irb(main):048:0> /(?!^admin$).{5,5}/i =~ "xadmin"
=> 0
irb(main):049:0> /(?!^admin$).{5,5}/i =~ "xadminx"
=> 0
irb(main):050:0> /(?!^admin$).{5,5}/i =~ "adminx"

kind regards -botp

Match the route with /admin/ in the path first.

map.connect ':lang/admin/:controller/:action/:id

If it's matched, it won't go on and try to match the route that you
showed us.

This is really a RoR question.

-Kyle

···

On Apr 15, 8:42 am, "Jeff Schoolcraft" <jeffrey.schoolcr...@gmail.com> wrote:

I don't think you'll be able to do what you want with just a regex pattern.
The closest you could get is negative lookahead. You'd have to pull out the
first character and use negative lookahead for the rest, it will probably
only work for simple strings.

irb(main):029:0> a = "Hello World"
=> "Hello World"
irb(main):030:0> a =~ /h(?!ello)/i
=> nil
irb(main):031:0> b = "Hel l o world"
=> "Hel l o world"
irb(main):032:0> b =~ /h(?!ello)/i
=> 0

On Tue, Apr 15, 2008 at 9:17 AM, Dipesh Batheja <dipesh_bath...@yahoo.com> > wrote:

> Problem is i have function, which takes a regex as the parameter. I want
> to pass this regex in such a way that the function returns false if the
> string is matched and true if it isn't. I want the regex itself specify
> that if it matches the given string then it is false otherwise true.

> Jesús Gabriel y Galán wrote:
> > On Tue, Apr 15, 2008 at 2:59 PM, Dipesh Batheja > > > <dipesh_bath...@yahoo.com> wrote:
> >> Can somebody hep me with a very simple regular expression. I want to
> >> write an regex which if matches a specific string should return false.
> >> for example:
> >> if i have string "Hello world", and i want to say, if you find "hello"
> >> in this string then return false. Can someone explain how to write
> this?

> > Simple approach:

> > irb(main):007:0> a = "hello world"
> > => "hello world"
> > irb(main):008:0> !(a =~ /hello/)
> > => false
> > irb(main):009:0> !(a =~ /asdfsdf/)
> > => true

> > Jesus.

> --
> Posted viahttp://www.ruby-forum.com/.

--
Jeff Schoolcrafthttp://thequeue.net/blog/
Microsoft MVP

Hi --

···

On Wed, 16 Apr 2008, Kyle wrote:

Match the route with /admin/ in the path first.

map.connect ':lang/admin/:controller/:action/:id

If it's matched, it won't go on and try to match the route that you
showed us.

This is really a RoR question.

No, just an RoR answer :slight_smile:

David

--
Rails training from David A. Black and Ruby Power and Light:
   INTRO TO RAILS June 9-12 Berlin
   ADVANCING WITH RAILS June 16-19 Berlin
   INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!