Jump to content
Sign in to follow this  
lsakizada

Extract date from string by using StringRegExpReplace

Recommended Posts

Hi,

I need help with regexp to extract the date from a file name, because I do not much about regexp  : .

The file name has the following naming template:

VIAS1_2015_02_15_part_02.log

The file name can be any like this (where XX and YY can be changed):

VIASXX_2015_02_15_part_YY.log

I needs the following values to be extracted:

$Year=2015
$Month = 02
$Day = 15

Best Regards,


Be Green Now or Never (BGNN)!

Share this post


Link to post
Share on other sites

one way...

#include <array.au3>

local $str = 'VIAS1_2015_02_15_12_02.log'

local $a10 = stringregexp($str,'_(\d+)_(\d+)_(\d+)',3)

_arraydisplay($a10)

local $year = $a10[0]
local $mnth = $a10[1]
local $day  = $a10[2]

ConsoleWrite(stringformat('%4s/%2s/%2s',$year,$mnth,$day) & @CRLF)

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

another way...

local $str = '123_2015_02_15_12_02.log'

local $year = stringregexpreplace($str,'.*_(\d{4})_\d?\d_\d?\d+.*','$1')
local $mnth = stringregexpreplace($str,'.*_\d{4}_(\d?\d)_\d?\d+.*','$1')
local $day = stringregexpreplace($str,'.*_\d{4}_\d?\d_(\d?\d+).*','$1')

ConsoleWrite(stringformat('%4s/%2s/%2s',$year,$mnth,$day) & @CRLF)

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

If you do it like this:

local $str = '123_2015_02_15_12_02.log'
local $date = stringregexpreplace($str,'.*_(\d{4})_(\d?\d)_(\d?\d+)_(\d?\d+)_(\d?\d+).*','$1/$2/$3 $4:$5')

... you get a date and time you can parse through Date.au3's _DateTimeFormat.


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Hi Mikell,

That's exactly what I have done , but because I have many calls during single call I thought it will be faster to use regex

correction:

$s = StringSplit($StrFN, "_")
Msgbox(0,"", $s[3] & "/" & $s[4] & "/" & $s[5])

Regards

Edited by lsakizada

Be Green Now or Never (BGNN)!

Share this post


Link to post
Share on other sites

Oh you are right

A fragmented solution like the one in your post #3 then

Local $str = 'something_123_2015_02_15_12_else_02.log'

Msgbox(0,"year", stringregexpreplace($str, '.*(\d{4})_\d{2}_\d{2}.*', "$1") )
Msgbox(0,"month", stringregexpreplace($str, '.*\d{4}_(\d{2})_\d{2}.*', "$1") )
Msgbox(0,"day", stringregexpreplace($str, '.*\d{4}_\d{2}_(\d{2}).*', "$1") )

Share this post


Link to post
Share on other sites

#8 + StringSplit would probably be faster

But then again, if StringSplit was working, then it's going to be much faster than regex calls.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...