Jump to content

Recommended Posts

Posted

hello world!

i have strings that vary positioning for superfluous commas - sometimes extra commas in the middle, beginning or end of the string

this is the method im using to clean up the string - but i dont think its the cleanest solution

any ideas?  stringregexp perhaps?
 
Thanks in advance!
#include <Array.au3>
$msg_normal = 262144


$string = ",,,,asdf,as32,,,3k2,as12,"


$array = StringSplit($string, ",")


debug($array)


$new_string = ""


for $x = 1 to UBound($array)-1
if $array[$x] = "" then ContinueLoop


$new_string &= $array[$x] & ","
Next


$new_string = StringTrimRight($new_string, 1)


debug($new_string)


Func Debug($variable1 = "", $variable2 = "", $variable3 = "")


;~  #include <array.au3>
;~  $msg_normal = 0


If IsArray($variable1) Then
_ArrayDisplay($variable1)
Else
If $variable2 <> "" Then
$variable1 &= @CRLF & $variable2
EndIf


If $variable3 <> "" Then
$variable1 &= @CRLF & $variable3
EndIf


ClipPut($variable1)
MsgBox($msg_normal, "Debug", $variable1)
EndIf


EndFunc   ;==>Debug
 

 

Posted

What's your final output expected to be?  If this "asdf,as32,3k2,as12", then

$string = ",,,,asdf,as32,,,3k2,as12,"
$string2 = StringRegExpReplace($string,",+",",")
$string2 = StringRegExpReplace($string2,"\B,|,\B","")
ConsoleWrite($string2 & @CRLF)
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Posted (edited)

it is - sorry should have mentioned that  :idiot:

thank you very much!!

Edited by gcue
Posted

what if the character was | instead of comma?  i tried replacing the character but doesnt work.. 

Posted

You would need to know how to backtrack in the search, which I don't, so I didn't.  If you could find a way to find all three situations, and replace with the exact same result, then you could do a simple regexpreplace.

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Posted (edited)

hmm i dont either -  i think i may just resort back to my original method

Func StripSuperfluousCharacter($string, $character)


$array = StringSplit($string, $character)


$new_string = ""


for $x = 1 to UBound($array)-1
if $array[$x] = "" then ContinueLoop


$new_string &= $array[$x] & $character
Next


$new_string = StringTrimRight($new_string, 1)


return $new_string


EndFun

thanks again for your help =)

Edited by gcue
Posted (edited)

#include <Array.au3>

$string = ",,,,asdf,as32,,,3k2,as12,"
$delimeter = ","
$aString = stringsplit($string , "" , 3)

If $aString[ubound($aString) - 1] = $delimeter then _ArrayPop($aString)

For $i = ubound($aString) - 1 to 1 step -1
    If $aString[$i] = $delimeter AND $aString[$i - 1] = $delimeter Then _ArrayDelete($aString , $i)
Next

If $aString[0] = $delimeter then _ArrayDelete($aString , 0)

msgbox(0 , '' , _ArrayToString($aString , ""))

Edited by boththose

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

Posted

A regex way :

; $string = ",,,,asdf,as32,,,3k2,as12,"
; $delimeter  = ","


$string = "||||asdf|as32|||3k2|as12|"
$delimeter  = "|"

$string2 = StringRegExpReplace($string, "(\Q" & $delimeter & "\E)(?=(?1)|$)","")
ConsoleWrite($string2 & @CRLF)
Posted

 

A regex way :

; $string = ",,,,asdf,as32,,,3k2,as12,"
; $delimeter  = ","


$string = "||||asdf|as32|||3k2|as12|"
$delimeter  = "|"

$string2 = StringRegExpReplace($string, "(\Q" & $delimeter & "\E)(?=(?1)|$)","")
ConsoleWrite($string2 & @CRLF)

 

works great... except it doesnt remove the leading instance of the delimiter

$string = "||||asdf|||||||||||||||||||||||||||||||||||||||||||||||||||||||as32|3k2|as12||||||||"
$delimeter  = "|"


$string2 = StringRegExpReplace($string, "(\Q" & $delimeter & "\E)(?=(?1)|$)","")


debug($string2)
Posted

so add one more line?  JG, that is a nice regex btw.

$string2 = StringRegExpReplace($string, "(\Q" & $delimeter & "\E)(?=(?1)|$)","")
If stringleft($string2 , 1) = $delimeter Then $string2 = stringtrimleft($string2, 1)


msgbox(0 , '' , $string2)

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

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
  • Recently Browsing   0 members

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