Jump to content

RegExpReplace -- Remove whitespace Before lines of text


2Toes
 Share

Recommended Posts

Hello all B)

For the past week or so, I've been learning & messing around with Regular Expressions.

Despite the amount that I've learned throughout this time, I seem to be having an issue with what would seem to be a simple task..

I'm trying to remove gaps of Whitespace that sit in Front of text, as well as empty lines between lines of text.

Here's the expression that I've come up with thus far:

(?is)(?<=^)\s*(?=\w)*

The logic that I'm shooting for is:

Grab 0 or More Whitespaces - between [HAT] and the 1st word Character - and do that 0 or More times.

 

Here's a simple sample code I put together:

$text = "   This is Line 1" & @CRLF & _
"           " & @CRLF & _
"                   " & @CRLF & _
"               This is Line 4  " & @CRLF & _
"                           " & @CRLF & _
"       " & @CRLF & _
"                                                                           This is Line 7" & @CRLF & _
"                                   " & @CRLF & _
"               " & @CRLF & _
"           This is Line 10                 " & @CRLF & _
"   " & @CRLF & _
"                   " & @CRLF & _
"                                       This is Line 13"


$text = StringRegExpReplace($text, "(?is)(?<=^)\s*(?=\w)*", "")

ConsoleWrite($text)

 

After running the code, the gap of Whitespace in front of "This is Line 1" gets removed..

But all other gaps of whitespace remain in place.

 

Does anyone have any ideas on what might be the issue here?

 

Any tips or hints would be greatly appreciated :)

Link to comment
Share on other sites

remove the leading spaces, and then any two whitespaces (or more) in a row replace with a single LF?

$text = "   This is Line 1" & @CRLF & _
"           " & @CRLF & _
"                   " & @CRLF & _
"               This is Line 4  " & @CRLF & _
"                           " & @CRLF & _
"       " & @CRLF & _
"                                                                           This is Line 7" & @CRLF & _
"                                   " & @CRLF & _
"               " & @CRLF & _
"           This is Line 10                 " & @CRLF & _
"   " & @CRLF & _
"                   " & @CRLF & _
"                                       This is Line 13"

$text= StringRegExpReplace(StringStripWS($text , 1) , "(\s\s+)" , @LF)

ConsoleWrite($text)

 

Edited by iamtheky

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

Link to comment
Share on other sites

Try that as well:

$text = "   This is Line 1" & @CRLF & _
"           " & @CRLF & _
"                   " & @CRLF & _
"               This is Line 4  " & @CRLF & _
"                           " & @CRLF & _
"       " & @CRLF & _
"                                                                           This is Line 7" & @CRLF & _
"                                   " & @CRLF & _
"               " & @CRLF & _
"           This is Line 10                 " & @CRLF & _
"   " & @CRLF & _
"                   " & @CRLF & _
"                                       This is Line 13"

$text = StringRegExpReplace($text, "(?m)^\R|^\s+(?=\S)", "")
ConsoleWrite($text & @LF)

 

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)

Link to comment
Share on other sites

Hey @jchd

 

Thanks for jumping in with that. You have a couple things in there that I hadn't learned yet -- (?m) and \R

So, if I'm understanding that correctly..

(?m) switched on MultiLine.

Then, it matches any Empty lines .. ^\R

OR

it matches any whitespace that's between [HAT] and anything that's NOT a whitespace .. (?=\S)

And then of course replaces them with ""

 

Very interesting. Thanks again for that. Much appreciated!! :D

Link to comment
Share on other sites

Good reading.

For a full, strict and correct explanation of a PCRE-compatible regex in verbose plain English you can use http://regex101.com/ which also offers a useful step-by-step debugging feature.

Sidenote: AutoIt PCRE implementation sets \R to match LF or CR or CRLF. That can be changed at runtime (see help).

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)

Link to comment
Share on other sites

With the regex search string „(?m)^\R|^\s+(?=\S)|\h*$“ you can delete all horizontal whitespaces between the last word in line and the line end.

Simpel

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
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
 Share

  • Recently Browsing   0 members

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