Stuart C. wrote:
All this data is within event.description. My regular expression goes in
and grabs only Logon Type: 2.
I can assure you the only way I have been able to get this is by
#{event.description[/Logon Type:\t2/]}
event is some object
event.description is a string
So event.description[…] is calling the [] method on this string. This
is often referred to as the String#[] method (i.e. the instance method
‘[]’ on class String)
[] is one of those methods which can be called in a syntactic-sugary
way.
“abcdef”. # => “cd” - # direct call of [] method
“abcdef”[2,2] # => “cd” # same using [] method sugar
In any case, you’re not creating an array here. Rather you’re passing a
Regexp to the String#[] method.
If I change the expression to #{event.description(/Logon Type:\t2/)}.
It doesn’t work (I dont no why, guess its something to do with library).
It’s because you’re then trying to pass an argument to the
event.description() method. Note that
event.description
is the same as
event.description() # method call with zero arguments
but is not the same as
event.description(/foo/) # method call with one argument
You should have gotten an error message (“ArgumentError: wrong number of
arguments (1 for 0)” or something like that)
To clarify I want to find Logon Type: 2 in the data set and change it to
Local logon.
As others have said, the methods to look at are:
String#[]= # modifies the string in-place
String.gsub! # modifies the string in-place
String.gsub # returns a new string
All these can take a simple string or a Regexp as the expression to
search for.
Hope this helps, I do understand what you are saying with the reg
expressions but, all i can tell you is it works.
You need to beware of things which look similar, but which are not.
foo[bar] # call of method '[]' on foo, 1 argument
[bar] # an array literal - 1-element array
# containing bar
foo.baz(bar) # call of method 'baz' on foo, 1 arg
# which is bar
foo.baz([bar]) # call of method 'baz' on foo, 1 arg
# which is 1-element array containing
bar
In any case, ‘irb’ is the best place to try these things out.
irb(main):001:0> a = “hello”
=> “hello”
irb(main):002:0> a[“el”] = “EL”
=> “EL”
irb(main):003:0> a
=> “hELlo”
irb(main):004:0> a.gsub(/EL/,‘el’)
=> “hello”
irb(main):005:0> a # Note: a is unchanged
=> “hELlo”
irb(main):006:0> a.gsub!(/EL/,‘el’)
=> “hello”
irb(main):007:0> a
=> “hello”