Change captured values in a regexp

B

Bruno Sousa

Hi everyone!
Supposing there is a string like this:
Oct 22 06:25:35 machine kernel: [271182.956004] IN= OUT=eth0
SRC=10.8.0.248 DST=192.5.5.241

I am using the following regexp to capture time:
/Oct (\d{2}) (\d{2}:\d{2}:\d{2})/

How can I alter the hour value only and keep it in the result returned?
I want to decrease 3 hours.
Instead of #<MatchData "Oct 22 06:25:35" 1:"22" 2:"06:25:35">
It should return #<MatchData "Oct 22 06:25:35" 1:"22" 2:"03:25:35">
 
W

w_a_x_man

Hi everyone!
Supposing there is a string like this:
Oct 22 06:25:35 machine kernel: [271182.956004] IN= OUT=eth0
SRC=10.8.0.248 DST=192.5.5.241

I am using the following regexp to capture time:
/Oct (\d{2}) (\d{2}:\d{2}:\d{2})/

How can I alter the hour value only and keep it in the result returned?
I want to decrease 3 hours.
Instead of #<MatchData "Oct 22 06:25:35" 1:"22" 2:"06:25:35">
It should return  #<MatchData "Oct 22 06:25:35" 1:"22" 2:"03:25:35">

What is going to happen when you subtract 3 hours from a date
like this:
Oct 01 02:25:35

require 'date'
==>true
t=DateTime.parse("Oct 01 02:25:35") - Date.time_to_day_fraction(3,0,0)
==>#<DateTime: 42430529827/17280,0,2299161>
t.strftime("%b %d %H:%M:%S")
==>"Sep 30 23:25:35"
 
A

Ammar Ali

Hi everyone!
Supposing there is a string like this:
Oct 22 06:25:35 machine kernel: [271182.956004] IN=3D OUT=3Deth0
SRC=3D10.8.0.248 DST=3D192.5.5.241

I am using the following regexp to capture time:
/Oct (\d{2}) (\d{2}:\d{2}:\d{2})/

How can I alter the hour value only and keep it in the result returned?
I want to decrease 3 hours.
Instead of #<MatchData "Oct 22 06:25:35" 1:"22" 2:"06:25:35">
It should return =C2=A0#<MatchData "Oct 22 06:25:35" 1:"22" 2:"03:25:35">

That's not a job for regular expressions. You need to handle this by
yourself. Once you have the hour match, convert it into a Time object
(see http://ruby-doc.org/core/classes/Time.html#M000305) and then
offset it as you need.

HTH,
Ammar
 
R

Robert Klemme

Supposing there is a string like this:
Oct 22 06:25:35 machine kernel: [271182.956004] IN= OUT=eth0
SRC=10.8.0.248 DST=192.5.5.241

I am using the following regexp to capture time:
/Oct (\d{2}) (\d{2}:\d{2}:\d{2})/

How can I alter the hour value only and keep it in the result returned?
I want to decrease 3 hours.
Instead of #<MatchData "Oct 22 06:25:35" 1:"22" 2:"06:25:35">
It should return #<MatchData "Oct 22 06:25:35" 1:"22" 2:"03:25:35">

You don't want to mess with what the regexp match returns. Rather you
need to process results afterwards. And please also consider the other
comments.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top