DaLiMan Posted September 18, 2017 Share Posted September 18, 2017 (edited) So after trying a couple of hours..... I've got this string to column expression down below from @junkew in an previous post. this works very well, but now I like to also replace the double spaces in column the columns. I've tried placing the \s or [:space:] withiin the brakeds like so (.{35}\s) or other variants. But then the whole just won't work anymore. Can it be done? Original code: $newString= StringRegExpReplace($File_String,'(.{1})(.{7})(.{259})(.{70})(.{187})(.{35})(.{64})','$2;$4,$6;') Failed while trying: $newString= StringRegExpReplace($File_String,'(.{1})(.{7})(.{259})(.{70})(.{187})((.{35})\s)(.{64})','$2;$4,$6;') $newString= StringRegExpReplace($File_String,'(.{1})(.{7})(.{259})(.{70})(.{187})((.{35})[:space:])(.{64})','$2;$4,$6;') Need this: 9899673;PROD NAME AAAAAA123456 ,213540 ; 9899680;PROD NAME BBBBBB123456 ,213540 ; 9899687;PROD NAME CCCCCC 123456 ,213540 ; 9899694;PROD NAME AAAAAA 789 ,213540 ; 9899757;PROD NAME BBBBBB 789 ,213540 ; To become: 9899673;PROD NAME AAAAAA123456;213540; 9899680;PROD NAME BBBBBB123456;213540; 9899687;PROD NAME CCCCCC 123456;213540; 9899694;PROD NAME AAAAAA 789;213540; 9899757;PROD NAME BBBBBB 789;213540; Edited September 18, 2017 by DaLiMan typo Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 18, 2017 Moderators Share Posted September 18, 2017 DaLiMan, I would do it this way: $sOriginal = "9899673;PROD NAME AAAAAA123456 ,213540 ;" & @CRLF & _ "9899680;PROD NAME BBBBBB123456 ,213540 ;" & @CRLF & _ "9899687;PROD NAME CCCCCC 123456 ,213540 ;" & @CRLF & _ "9899694;PROD NAME AAAAAA 789 ,213540 ;" & @CRLF & _ "9899757;PROD NAME BBBBBB 789 ,213540 ; " $sAmended = StringRegExpReplace($sOriginal, "(\s+)([,;])", "$2") ConsoleWrite($sAmended & @CRLF & @CRLF) $sCompare = "9899673;PROD NAME AAAAAA123456;213540;" & @CRLF & _ "9899680;PROD NAME BBBBBB123456;213540;" & @CRLF & _ "9899687;PROD NAME CCCCCC 123456;213540;" & @CRLF & _ "9899694;PROD NAME AAAAAA 789;213540;" & @CRLF & _ "9899757;PROD NAME BBBBBB 789;213540;" ConsoleWrite($sCompare & @CRLF) The RegEx removes all spaces followed by a comma or semicolon. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
DaLiMan Posted September 18, 2017 Author Share Posted September 18, 2017 15 minutes ago, Melba23 said: The RegEx removes all spaces followed by a comma or semicolon. Thanks Melba23, that works nicely, Can you explain to me what the "$2" means in the expression? Not good in RegEx and trying to understand. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 18, 2017 Moderators Share Posted September 18, 2017 (edited) DaLiMan, Sure: Pattern: (\s+) - capture first group with one or more spaces... ([,;]) - immediately followed by either a comma or semicolon, which we also capture Replacement: $2 - Replace both groups by the second group that we captured - i.e. remove the spaces All clear now? Please ask again if not. M23 Edited September 18, 2017 by Melba23 Fixed formatting Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Malkey Posted September 18, 2017 Share Posted September 18, 2017 (edited) Another attempt to get the "To become:" string of post #1, where all the commas are replaced with semicolons. Local $File_String = "9899673;PROD NAME AAAAAA123456 " & @CRLF & _ " ,213540 ;" & @CRLF & _ "9899680;PROD NAME BBBBBB123456 " & @CRLF & _ " ,213540 ;" & @CRLF & _ "9899687;PROD NAME CCCCCC 123456 " & @CRLF & _ " ,213540 ;" & @CRLF & _ "9899694;PROD NAME AAAAAA 789 " & @CRLF & _ " ,213540 ;" & @CRLF & _ "9899757;PROD NAME BBBBBB 789 " & @CRLF & _ " ,213540 ;" ConsoleWrite(StringRegExpReplace($File_String, "\s+[,;]", ";") & @CRLF) #cs ; Returns:- 9899673;PROD NAME AAAAAA123456;213540; 9899680;PROD NAME BBBBBB123456;213540; 9899687;PROD NAME CCCCCC 123456;213540; 9899694;PROD NAME AAAAAA 789;213540; 9899757;PROD NAME BBBBBB 789;213540; #ce Edited September 18, 2017 by Malkey Changed from: StringRegExpReplace($File_String, "(?m)(?:\h+\v+\h+,)(\d+)(\h+;)$", ";$1;") Link to comment Share on other sites More sharing options...
DaLiMan Posted September 18, 2017 Author Share Posted September 18, 2017 So the 2 in $2 stand for the second group. And when $1 was given it would remove all the [ ; ] or [ , ] ? Do I understand correctly? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 18, 2017 Moderators Share Posted September 18, 2017 DaLiman, You got it! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
DaLiMan Posted September 18, 2017 Author Share Posted September 18, 2017 Thanks to the both of you. @Malkey, Your code does not do exactly what I expected for the file I'm using. However, still many thanx for helping me out. Link to comment Share on other sites More sharing options...
mikell Posted September 18, 2017 Share Posted September 18, 2017 4 hours ago, Melba23 said: The RegEx removes all spaces followed by a comma or semicolon. Literally translated, it's exactly this StringRegExpReplace($sOriginal, "\s+(?=[,;])", "") Link to comment Share on other sites More sharing options...
iamtheky Posted September 26, 2017 Share Posted September 26, 2017 and without, because why not. I think this can be further reduced sans regex Local $File_String = "9899673;PROD NAME AAAAAA123456 " & @CRLF & _ " ,213540 ;" & @CRLF & _ "9899680;PROD NAME BBBBBB123456 " & @CRLF & _ " ,213540 ;" & @CRLF & _ "9899687;PROD NAME CCCCCC 123456 " & @CRLF & _ " ,213540 ;" & @CRLF & _ "9899694;PROD NAME AAAAAA 789 " & @CRLF & _ " ,213540 ;" & @CRLF & _ "9899757;PROD NAME BBBBBB 789 " & @CRLF & _ " ,213540 ;" ConsoleWrite(stringreplace(stringreplace(stringreplace(StringStripWS($File_String , 4) , " ," , ";") , "NAME " , "NAME " , 0 , 1) , " ;" , ";")) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
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