Jump to content
Sign in to follow this  
iCode

strip trailing whitespace from multi-line string

Recommended Posts

iCode

i'm trying to strip any trailing space from every line of a multiline string without splitting the string using something like StringRegExpReplace($string, '(?m)\h+$', '')

sample string...

a [followed by a space]

b [followed by a space]

so it seems like any of these expressions should work, but only the last space is matched...

(?m)\h+$
(?m:\h+$)
(?m:\h+)$
(?m)\h+\z
(?m:\h+\z)
(?m:\h+)\z

so \h doesn't seem like it's working in multiline mode, yet this does...

(?m)\s+$

from the help file...

$ assert end of string (or line, in multiline mode)

and...

The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and PCRE_EXTENDED options can be changed from within the pattern by a sequence of Perl option letters enclosed between "(?" and ")". The option letters are

m for PCRE_MULTILINE


FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites
Ramzes

I think this is the easiest way:

#include <Array.au3>
$String = "Red   " & @CRLF & "Blue " & @CRLF & "Green     "
$String = StringSplit($String, @CRLF, 3)
For $i = 0 To UBound($String) - 1
$String[$i] = StringStripWS($String[$i], 2)
Next
$String = _ArrayToString($String, @CRLF)
MsgBox(32, "", "|" & $String & "|")

Sorry for my bad English but nobody is perfect. [font=arial, helvetica, sans-serif]Ramzes[/font]

Share this post


Link to post
Share on other sites
jchd

StringRegExpReplace($s, '\h*\r', '')


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Melba23

iCode,

Try this: :graduated:

StringRegExpReplace($sString, "\h*(?=\v|\z)", "")

\h*      - any number of horizontal whitespace characters
(?=\v|\z) - which are followed by either a vertical whitespce or an end of string.

That seems to work even if there are multiple spaces at the end of the lines or additional spaces within them. ;)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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
iCode

i should have specified that i don't want to strip vertical spaces, so none of the above will work, except...

@Melba23 - ?= positive look ahead is a weak point for me - i changed yours a bit (* to +) and this seems to doing what i want...

\h+(?=\v|\z)

thanks for that! :graduated:

so this is is pretty close to what i wanted to build - matches string beginning space(s), string ending space(s) and beginning and ending horiz space(s) for each line...

(?m:^\h+)|\h+(?=\v|\z)

would be cool i think to have 2 options added to StringStripWS()

* strip all horizontal space for beginning of every line

* strip all horizontal space for end of every line

actually this works even better...

^\s+|(?m:^\h+)|\h+(?=\v|\z)|\s+$
Edited by iCode

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites
Melba23

iCode,

You can do that with a simple wrapper function as Ramzes showed above so I doubt your suggestion woudl meet with much success. :graduated:

From the Feature Request Guidelines:

Think about your feature request. If you can do it via UDFs, then don't ask for it to be built in. There's a reason you can create your own functions.

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

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
Sign in to follow this  

×