DaLiMan

StringRegExpReplace double space in columns

10 posts in this topic

#1 ·  Posted (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 by DaLiMan
typo

Share this post


Link to post
Share on other sites



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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
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. :>

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by Malkey
Changed from: StringRegExpReplace($File_String, "(?m)(?:\h+\v+\h+,)(\d+)(\h+;)$", ";$1;")

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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+(?=[,;])", "")

:)

Share this post


Link to post
Share on other sites

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) , " ;" , ";"))

 


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

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