Trim sentences

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Billykid
Posts: 98
Joined: 16 Sep 2019, 08:59

Trim sentences

16 Apr 2024, 10:22

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.
User avatar
mikeyww
Posts: 27010
Joined: 09 Sep 2014, 18:38

Re: Trim sentences

16 Apr 2024, 10:52

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
User avatar
Chunjee
Posts: 1423
Joined: 18 Apr 2014, 19:05
Contact:

Re: Trim sentences

16 Apr 2024, 11:00

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."

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: DaveF and 408 guests