Sign in to follow this  
Followers 0
Marzupilami

Excluding unknown letters/numbers in StringReplace

6 posts in this topic

Hi.

I have this line of code:

$title_A = StringReplace($string_A[0], "[xxx]", "")

Instead of xxx, what command can I use to exclude like the first three letters, the first five, and so on? If it's a random date for example (21 dec 17:36) and I only want the hours (17), then I need to exclude the minutes and date and so on even though I don't know numbers it is? And it's not the current date so @HOUR won't work. How should I do?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You need to use a regular expression, example :

#Include <Array.au3>

$str = "21 dec 17:36"

$res = StringRegExp($str, '(\d+)\s*(\w+)\s*(\d+):(\d+)', 3)
 _ArrayDisplay($res)

$res1 = StringRegExp($str, '(\d+):', 3)
 _ArrayDisplay($res1)

Of course usual String* funcs also work

$str = "21 dec 17:36"
Msgbox(0,"", StringMid($str, 8, 2) )
Edited by mikell
1 person likes this

Share this post


Link to post
Share on other sites

a couple of passes with stringtrim

msgbox (0 , '' , _Hour("21 dec 17:36"))
msgbox (0 , '' , _Hour("2 nov 3:36"))
msgbox (0 , '' , _Hour("9 oct 6:00"))


Func _Hour($sString)

$sStrip1 = stringtrimright(stringtrimleft($sString , 5) , 3)

If stringleft($sStrip1 , 1) = " " Then
    $sStrip2 = stringtrimleft($sStrip1 , 1)
Else
    $sStrip2 = stringtrimleft($sStrip1 , 2)
EndIf

Return $sStrip2

EndFunc
1 person likes this

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

a little improvement could be like this:
(pass the date and the part you want to be returned as:
0=day
1=month
2=minutes
3=seconds)
also easly modifiable if date format change, for example also year is included.

MsgBox(0, "Day", _SplitDate("25 dec 23:59", 0)) ; return day
MsgBox(0, "Month", _SplitDate("21 dec 17:36", 1)) ; return month
MsgBox(0, "Minutes", _SplitDate("2 nov 3:36", 2)) ; return minutes
MsgBox(0, "Seconds", _SplitDate("9 oct 6:00", 3)) ; return seconds

Func _SplitDate($sString, $iPart = 0)
    Local $aParts = StringSplit(StringStripWS($sString, 7), " :", 2)
    Return $aParts[$iPart]
EndFunc   ;==>_SplitDate
Edited by Chimp
1 person likes this

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

 

a little improvement could be like this:

(pass the date and the part you want to be returned as:

0=day

1=month

2=minutes

3=seconds)

also easly modifiable if date format change, for example also year is included.

MsgBox(0, "Day", _SplitDate("25 dec 23:59", 0)) ; return day
MsgBox(0, "Month", _SplitDate("21 dec 17:36", 1)) ; return month
MsgBox(0, "Minutes", _SplitDate("2 nov 3:36", 2)) ; return minutes
MsgBox(0, "Seconds", _SplitDate("9 oct 6:00", 3)) ; return seconds

Func _SplitDate($sString, $iPart = 0)
    Local $aParts = StringSplit(StringStripWS($sString, 7), " :", 2)
    Return $aParts[$iPart]
EndFunc   ;==>_SplitDate

Nice, even though I don't understand it..

How can I do the same thing with hours?

Share this post


Link to post
Share on other sites

Nice, even though I don't understand it..

How can I do the same thing with hours?

 

... sorry, you are right, I write "minutes" and "seconds" where them are "hours" and "minutes" instead... it's just a typo, but it works as well,

passing 2 as second parameter it returns hours (and not minutes)

passing 3 it returns minutes (and not seconds)

sorry :)

in short it works like this:

stringsplit() "dissects" your input string passing each part separated by a space or a colon in single elements into an array

where first part on the left of your string (the day) goes to the first element of the array (first element is element 0) $aParts[0] in this case... and so on

1 person likes this

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

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  
Followers 0