NoizeBit

How to trim string starting and ending with a certain character?

13 posts in this topic

Hi,

I'd like to trim this line:

ue="1001372">27/07 05:00<

to look like this:

27/07 05:00

was using this until I've found out the numbers within the quotation marks are changing from time to time (sometimes there's one more number):

StringTrimRight(StringTrimLeft('ue="1001372">27/07 05:00<', 13), 1)

So how should I trim this string's left side starting from "u" till ">"?

Share this post


Link to post
Share on other sites



You can try RegExp

$var = 'ue="1001372">27/07 05:00<'
MsgBox(0,'',Trim($var))

Func Trim($var)
    Local $match = StringRegExp($var,'(.*?)(\d{2})(\/)(\d{2})(\s)(\d{2})(:)(\d{2})',1)
    Local $result = ''
    If IsArray($match) Then
        For $i = 1 To UBound($match)-1
            $result &= $match[$i]
        Next
    EndIf
    Return $result
EndFunc

 

1 person likes this

When the words fail... music speaks

Share this post


Link to post
Share on other sites

or also like this:

$sString = 'ue="1001372">27/07 05:00<'
;
$sOutput = StringLeft(StringRight($sString, 12), 11)
;
MsgBox(0, 0, "Input:" & @TAB & $sString & @CRLF & "Out  :" & @TAB & $sOutput)

 

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

You can try RegExp

$var = 'ue="1001372">27/07 05:00<'
MsgBox(0,'',Trim($var))

Func Trim($var)
    Local $match = StringRegExp($var,'(.*?)(\d{2})(\/)(\d{2})(\s)(\d{2})(:)(\d{2})',1)
    Local $result = ''
    If IsArray($match) Then
        For $i = 1 To UBound($match)-1
            $result &= $match[$i]
        Next
    EndIf
    Return $result
EndFunc

 

Thanks Andreik, but I can't say I understand much from your code, but it's my fault, I'm just learning how to code :)

Share this post


Link to post
Share on other sites

or also like this:

$sString = 'ue="1001372">27/07 05:00<'
;
$sOutput = StringLeft(StringRight($sString, 12), 11)
;
MsgBox(0, 0, "Input:" & @TAB & $sString & @CRLF & "Out  :" & @TAB & $sOutput)

 

Thanks Chimp, this is more likely what I'd choose ;)

Isn't there an easier method which just simply deletes part of the string? It'll always start with "u" character and end with ">", but the string might change to just this: "05:00".

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

for sure some regex guru will provide a magic solution, anyway in the while this "workaround" could be quick a way. :)

$sString = 'ue="1001372">27/07 05:00<'
;
ConsoleWrite(StringReplace($sString, StringLeft($sString, StringInStr($sString, ">")), "") & @CRLF)

edit:

or this if the "u" char is not always at first position:

$sString = 'ue="1001372">27/07 05:00<'
;
ConsoleWrite(StringReplace($sString, StringMid($sString, StringInStr($sString, "u"), StringInStr($sString, ">")), "") & @CRLF)

 

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
$before = 'ue="1001372">27/07 05:00<'
$after = StringSplit($before, "<>")[2]
MsgBox(262144, "Result", $before & @CRLF & $after, 0)

 

Exactly what I was looking for, thanks ^_^

Share this post


Link to post
Share on other sites

Or _StringBetween...

#include <String.au3>
$before = 'ue="1001372">27/07 05:00<'
$after = _StringBetween($before, ">", "<")[0]
MsgBox(262144, "Result", $before & @CRLF & $after, 0)

 

1 person likes this

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Or _StringBetween...

#include <String.au3>
$before = 'ue="1001372">27/07 05:00<'
$after = _StringBetween($before, ">", "<")[0]
MsgBox(262144, "Result", $before & @CRLF & $after, 0)

 

Great, thanks :)

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Some regex...

$before = 'ue="1001372">27/07 05:00<'

$after = StringRegExpReplace($before, '.*>([^<]+).*', "$1")
MsgBox(0, "Result1", $before & @CRLF & $after)

; this way works but will crash your script in case of error (same for SadBunny's code)
$after = StringRegExp($before, '>([^<]+)', 3)[0]
MsgBox(0, "Result2", $before & @CRLF & $after)


;error example:
#include <String.au3>
$before = 'ue="1001372">27/07 05:00'  ; < is absent, so ...
$after = _StringBetween($before, ">", "<")[0]  ; ... no match causes AutoIt fatalll error !
MsgBox(262144, "Result", $before & @CRLF & $after, 0)

 

Edited by mikell
1 person likes this

Share this post


Link to post
Share on other sites

Some regex...

$before = 'ue="1001372">27/07 05:00<'

$after = StringRegExpReplace($before, '.*>([^<]+).*', "$1")
MsgBox(0, "Result1", $before & @CRLF & $after)

; this way works but will crash your script in case of error (same for SadBunny's code)
$after = StringRegExp($before, '>([^<]+)', 3)[0]
MsgBox(0, "Result2", $before & @CRLF & $after)


;error example:
#include <String.au3>
$before = 'ue="1001372">27/07 05:00'  ; < is absent, so ...
$after = _StringBetween($before, ">", "<")[0]  ; ... no match causes AutoIt fatalll error !
MsgBox(262144, "Result", $before & @CRLF & $after, 0)

 

Thank you, I'll try this one out, too!

Share this post


Link to post
Share on other sites

here one more way (are allowed also the presence of more groups on the source string, in this way you could extract all occurrences from a file in one shot)

#include <array.au3> ; just to show result(s)
Local $sOpen = ">", $sClose = "<" ; wanted Opening and Closing "delimiters"

$sSoure = 'ue="1001372">27/07 05:00<'
$aOut = StringRegExp($sSoure, "(?s)" & $sOpen & "(.*?)" & $sClose, 3) ; <- extraction
if IsArray($aOut) Then _ArrayDisplay($aOut, "From first string")

$sSoure = 'ue="1001372">27/07 05:00< ue=" 8013">28/07 04:00< ue=" 12345001372">29/07 03:00<the end'
$aOut = StringRegExp($sSoure, "(?s)" & $sOpen & "(.*?)" & $sClose, 3) ; <- extraction
if IsArray($aOut) Then _ArrayDisplay($aOut, "From second string")

 

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