gcue Posted February 4, 2015 Posted February 4, 2015 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! expandcollapse popup#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
jdelaney Posted February 4, 2015 Posted February 4, 2015 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.
gcue Posted February 4, 2015 Author Posted February 4, 2015 (edited) it is - sorry should have mentioned that thank you very much!! Edited February 4, 2015 by gcue
gcue Posted February 4, 2015 Author Posted February 4, 2015 what if the character was | instead of comma? i tried replacing the character but doesnt work..
jdelaney Posted February 4, 2015 Posted February 4, 2015 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.
gcue Posted February 4, 2015 Author Posted February 4, 2015 (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 February 4, 2015 by gcue
iamtheky Posted February 4, 2015 Posted February 4, 2015 (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 February 4, 2015 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
jguinch Posted February 4, 2015 Posted February 4, 2015 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) iamtheky 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
gcue Posted February 4, 2015 Author Posted February 4, 2015 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)
iamtheky Posted February 4, 2015 Posted February 4, 2015 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) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
mikell Posted February 4, 2015 Posted February 4, 2015 (edited) $string2 = StringRegExpReplace($string, "^(\Q" & $delimeter & "\E)*|(?1)(?=(?1)|$)","") Edited February 4, 2015 by mikell
jguinch Posted February 4, 2015 Posted February 4, 2015 (edited) Try this one : $string2 = StringRegExpReplace($string, "^(\Q" & $delimeter & "\E)+|(?1)(?=(?1))|(?1)+$","") Edit : oops, Mikell made the compact version... Edit2 : thanks boththose ! Edited February 4, 2015 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
mikell Posted February 4, 2015 Posted February 4, 2015 Yo my friend Twas just a slight modification of yours...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now