Page 1 of 1

Trim sentences

Posted: 16 Apr 2024, 10:22
by Billykid

Code: Select all

str := "        This is the first sentence.   `n   This is the second.    `n      Here comes the the third sentence.       "

str2 := ""
Loop parse, str , `n
    str2 .= Trim(A_LoopField) "`n"

MsgBox % str "`n`n" str2
ExitApp

How can I trim the sentences with a regular expression to get the same result?
Thanks for your help.

Re: Trim sentences

Posted: 16 Apr 2024, 10:52
by mikeyww

Code: Select all

#Requires AutoHotkey v1.1.33.11
str   := "        This is the first sentence.   `n   This is the second.    `n      Here comes the the third sentence.       "
regex := "m`a)^\h*(.+?)\h*$"
str2  := RegExReplace(str, regex, "$1")
MsgBox 64, Result, % Clipboard := str2

Re: Trim sentences

Posted: 16 Apr 2024, 11:00
by Chunjee
Just for fun:

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

str := "        This is the first sentence.   `n   This is the second.    `n      Here comes the the third sentence.       "

str2 := A.join(A.map(strSplit(str, "`n"), A.trim), "`n")
; => "This is the first sentence.`nThis is the second.`nHere comes the the third sentence."