Jump to content

Regex extract time from string


AutID
 Share

Recommended Posts

Hello,

 

I have a string where I need to extract the time from it. I am currently doing it with StringSplit but I'd love to see other versions.  Here is my way.

Local $sText = '2016/10/14 07:31:09">07:31'
Local $sSplit = StringSplit($sText, ':')
MsgBox(0, "", StringRight($sSplit[1], 2) & ":" & $sSplit[2] & ":" & StringLeft($sSplit[3], 2))

Anyone who has time to share some other knowledge is welcome.

 

Edit: I would like to see some regex version of it, not StringSplit versions :)

 

Edit2: Another way here:

Local $sText = '2016/10/14 07:31:09">07:31'
Local $sSplit = _StringBetween($sText, ' ', '"')[0]
MsgBox(0, "", $sSplit)

 

Edited by AutID
Link to comment
Share on other sites

As I'm not a regex expert:

Local $sText = '2016/10/14 07:31:09">07:31'
MsgBox(0, "", StringRegExpReplace($sText, '.*\h(.+?)".*', "$1"))

 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Hello. another example from a no regexp expert.

 

Local $sText = '2016/10/14 07:31:09">07:31'

MsgBox(0, "", _GetTime($sText))


Func _GetTime($String)
    Local $aReg = StringRegExp($String, '([0-9]+):([0-5][0-9]):([0-5][0-9])', 2)
    Return UBound($aReg) ? $aReg[0] : ""
EndFunc   ;==>_GetTime

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

one more, no regexp

Local $sText = '2016/10/14 07:31:09">07:31'
msgbox(0, '' , stringsplit(StringSplit($sText, ' ' , 2)[1] , '"' , 2)[0]))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

#include <Array.au3>
Local $sText = '2016/10/14 07:31:09">07:31'
$aArray = StringRegExp($sText, "([0-9]{4})/([0-9]{2})/([0-9]{2})[\s]+([0-9]{2}):([0-9]{2}):([0-9]{2})", 1)
_ArrayDisplay($aArray)
MsgBox(0, "", "DD/MM/YYYY:"&@TAB&$aArray[2]&"/"&$aArray[1]&"/"&$aArray[0]&@CRLF&"HH:MM:SS:"&@TAB&$aArray[3]&":"&$aArray[4]&":"&$aArray[5])

 

Link to comment
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
 Share

  • Recently Browsing   0 members

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