Forum: Ruby oddity with RegExp

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9528ea68a2efbf9093b1d41f7f269f36?d=identicon&s=25 Russell Fulton (russellf)
on 2007-08-02 00:04
I'm clearly missing something!  I have a log analysis application that
is written in Ruby and which uses REs to decide how to process log
records.

I entered a pattern into one of my rules and it always failed to match
so I fired up irb and set about reducing the pattern to something
minimal:

[rful011@pateke selms-svn]$ irb
irb(main):001:0> a = 'NT AUTHORITY\SYSTEM'
=> "NT AUTHORITY\\SYSTEM"
irb(main):004:0> a =~ /AUTHORITY/
=> 3
irb(main):005:0> a =~ /NT AUTHORITY/
=> 0
irb(main):006:0> a =~ / AUTHORITY/
=> 2
irb(main):007:0> a =~ /^NT /
=> 0
irb(main):008:0> a =~ /NT /
=> 0
irb(main):009:0> a =~ /NT/
=> 0
irb(main):010:0> a =~ /N/
=> 0
irb(main):011:0> a =~ /T/
=> 1


Is the '\' in the string upsetting things?

Cheers and thanks,

Russell
A3934c26868a038bd26764ea75dc7c42?d=identicon&s=25 Warren Brown (Guest)
on 2007-08-02 00:12
(Received via mailing list)
Russell,

> => "NT AUTHORITY\\SYSTEM"
> irb(main):009:0> a =~ /NT/
> => 0
> irb(main):010:0> a =~ /N/
> => 0
> irb(main):011:0> a =~ /T/
> => 1
>
> Is the '\' in the string upsetting things?

    No, you are just misinterpreting the results.

irb(main):001:0> a = 'NT AUTHORITY\SYSTEM'
=> "NT AUTHORITY\\SYSTEM"
irb(main):002:0> a =~ /N/
=> 0
irb(main):003:0> a =~ /X/
=> nil

    Zero is a valid string index, nil is not.

    - Warren Brown
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-08-02 00:12
(Received via mailing list)
Russell Fulton wrote:
> => "NT AUTHORITY\\SYSTEM"
> irb(main):009:0> a =~ /NT/
> => 0
> irb(main):010:0> a =~ /N/
> => 0
> irb(main):011:0> a =~ /T/
> => 1
I'm not sure what you're expecting here - all these patterns are
matching.  Have you got a failing pattern?
9528ea68a2efbf9093b1d41f7f269f36?d=identicon&s=25 Russell Fulton (russellf)
on 2007-08-02 01:13
Warren Brown wrote:

>
>     Zero is a valid string index, nil is not.
>
Thanks Warren!  Like I said I was clearly missing something. I blame my
perl heritage ;)

Russell
This topic is locked and can not be replied to.