AutID

Regex extract time from string

9 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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"))

 

1 person likes this

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!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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
1 person likes this

Share this post


Link to post
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]))

 


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

Share this post


Link to post
Share on other sites

here is my version :D 
 

Local $sText = '2016/10/14 07:31:09">07:31'
Local $Time = StringRegExp($sText, '\d{1,2}\:\d{1,2}\:\d{1,2}',1);
MsgBox(0, "", $Time[0])

 

Share this post


Link to post
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])

 

Share this post


Link to post
Share on other sites

So many ways to skin this cat ... :)

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

Local $s = StringRegExp($sText, '\H+(?=")', 1)
MsgBox(0, "", $s[0])

 

Share this post


Link to post
Share on other sites

:whistle:

Local $sText = '2016/10/14 07:31:09">07:31'
MsgBox(0, "", StringMid($sText, 12, 8))

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I'm totally going to flog Regexr.com here. This tool is absolutely brilliant for learning / testing regex. See this example: http://regexr.com/3efm3

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

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