Jump to content
Sign in to follow this  
HurleyShanabarger

RegExp Replace - Insert Whitespace

Recommended Posts

Hi there,

i have the following data:

WORD_1  :         RESULT_1
ANYLETTER_2                    :  RESULT_2
WORDNEXT_3            :        RESULT_3
SOMEWORDMORE_4          :         RESULT_4
ONLY_5      :           RESULT_6

The charset before whitespace and colon is know (can be matched using "\w+") but the length is different. The goal is to align the colons per row and also the words (again matchable using \w+) after the colon:

WORD_1         : RESULT_1
ANYLETTER_2    : RESULT_2
WORDNEXT_3     : RESULT_3
SOMEWORDMORE_4 : RESULT_4
ONLY_5         : RESULT_6

Align after the colon is easy, but is there any way to achieve it with a single regular expression?

StringRegExpReplace($Value, "(\w{6})\h+\:\h*(.*)", "$1         : $2", 0)

Thank you!

Share this post


Link to post
Share on other sites

A little more involved than a one-liner:

Local $sValue = _
"WORD_1  :         RESULT_1" & @CRLF & _
"ANYLETTER_2                    :  RESULT_2" & @CRLF & _
"WORDNEXT_3            :        RESULT_3" & @CRLF & _
"SOMEWORDMORE_4          :         RESULT_4" & @CRLF & _
"ONLY_5      :           RESULT_6"

Local $aValues = StringRegExp($sValue, "(\w+)[\h:]+(\w+)", 3)
Local $size
For $i = 0 To UBound($aValues) - 1 Step 2
    If StringLen($aValues[$i]) > $size Then $size = StringLen($aValues[$i])
Next
Local $sFormatted
For $i = 0 To UBound($aValues) - 1 Step 2
    $sFormatted &= StringFormat("%-" & $size & "s : %s\r\n", $aValues[$i], $aValues[$i + 1])
Next

ConsoleWrite($sFormatted)

No single regexp can scan the data to determinate the longest first word of each line and then go backwards to reformat stuff.

You can as well make the first regexp collect only first words but you still have to go thru the resulting array to chase the max size, like in code above. Of course you could do the same in a more pedestrian way with more code, keeping track of max size so far. Anyway, after that you can replace the second For loop by a StringRegexpReplace to do the formatting, in place of the second For loop. That's just a matter of taste.


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

Here is another method following the same logic as  jchd's example.

#include <String.au3>

Local $str = _
        "WORD_1  :         RESULT_1" & @CRLF & _
        "ANYLETTER_2                    :  RESULT_2" & @CRLF & _
        "WORDNEXT_3            :        RESULT_3" & @CRLF & _
        "SOMEWORDMORE_4          :         RESULT_4" & @CRLF & _
        "ONLY_5      :           RESULT_6"
        
Global $max = 0
Execute(StringRegExpReplace($str, "(?m)(\w+)(\h+.+\v*)", '__MaxLen(''$1'')& ') & "''") ; Get maximium number of characters of the first word in all lines.
;ConsoleWrite($max & @CRLF)

Local $sFormated = Execute(StringRegExpReplace($str, "(\w+)(\h+:\h+)(\w+\v*)", "'$1'&_StringRepeat(' ', $max - StringLen('$1'))& ' : $3'&") & "''")

ConsoleWrite($sFormated & @CRLF)


Func __MaxLen($sWord)
    If StringLen($sWord) > $max Then $max = StringLen($sWord)
EndFunc   ;==>__MaxLen

 

And, an abbreviation of the above script.

; https://www.autoitscript.com/forum/topic/197984-regexp-replace-insert-whitespace/?do=findComment&comment=1420097
Local $str = _
        "WORD_1  :         RESULT_1" & @CRLF & _
        "ANYLETTER_2                    :  RESULT_2" & @CRLF & _
        " WORDNEXT_3            :        RESULT_3 " & @CRLF & _
        "SOMEWORDMORE_4          :         RESULT_4" & @CRLF & _
        "ONLY_5      :           RESULT_6"

Local $max = 0

; Get $max, the maximium number of characters of the first word in all lines.
Execute(StringRegExpReplace($str, "(\w+).+\v*", '(Stringlen(''$1'')>$max?Assign("max",Stringlen(''$1'')):"")&') & "''")
;ConsoleWrite($max & @CRLF)

Local $sFormatted = Execute(StringRegExpReplace($str, "(\w+)\h*:\h*(\w+.*\v*)", 'StringFormat("%-"&$max&"s : %s","$1","$2")&') & "''")

ConsoleWrite($sFormatted & @CRLF)

 

Edited by Malkey
Added abbreviated script. Tidied RE pattern.

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...