lsakizada Posted February 22, 2015 Share Posted February 22, 2015 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)! Link to comment Share on other sites More sharing options...
kylomas Posted February 22, 2015 Share Posted February 22, 2015 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 Link to comment Share on other sites More sharing options...
kylomas Posted February 22, 2015 Share Posted February 22, 2015 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 Link to comment Share on other sites More sharing options...
SadBunny Posted February 22, 2015 Share Posted February 22, 2015 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. Link to comment Share on other sites More sharing options...
lsakizada Posted February 22, 2015 Author Share Posted February 22, 2015 Hi SadBunny and kylomas Thanks, I will check it later and let you know which one is appropriate. best regards Be Green Now or Never (BGNN)! Link to comment Share on other sites More sharing options...
mikell Posted February 22, 2015 Share Posted February 22, 2015 Tss Tss Don't forget the basics $str = "VIASXX_2015_02_15_part_YY.log" $s = StringSplit($str, "_") Msgbox(0,"", $s[2] & "/" & $s[3] & "/" & $s[4]) Link to comment Share on other sites More sharing options...
lsakizada Posted February 22, 2015 Author Share Posted February 22, 2015 (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 February 22, 2015 by lsakizada Be Green Now or Never (BGNN)! Link to comment Share on other sites More sharing options...
mikell Posted February 22, 2015 Share Posted February 22, 2015 Then a strict replace is the best Local $str = 'something_123_2015_02_15_12_else_02.log' Msgbox(0,"", stringregexpreplace($str, '.*(\d{4})_(\d{2})_(\d{2}).*', "$1/$2/$3") ) Trong 1 Link to comment Share on other sites More sharing options...
kylomas Posted February 22, 2015 Share Posted February 22, 2015 It looks like the OP wants date parts seperated, hence my solutions... 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 Link to comment Share on other sites More sharing options...
mikell Posted February 22, 2015 Share Posted February 22, 2015 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") ) Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 22, 2015 Moderators Share Posted February 22, 2015 #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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now