Jump to content

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


Recommended Posts

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 ">"?

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

 

Link to comment
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 :)

Link to comment
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".

Link to comment
Share on other sites

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

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

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

 

Edited by mikell
Link to comment
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!

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

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

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...