Jump to content

Recommended Posts

Posted

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)!

Posted

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

Posted

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

Posted

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.

Posted (edited)

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)!

  • Moderators
Posted

#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.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...