Regular Expression Problem Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Galenmacil
Posts: 2
Joined: 06 Aug 2016, 19:32

Regular Expression Problem

11 Dec 2023, 03:18

Hello,

I am about to give up on this simple regular expression substitution that work fine anywhere else but not with AutoHotkey:

Code: Select all

Text := "This is a test to see`nhow removing splitted lines using`nregular expression looks like when written to a log:`n`nExample 1`n`n`nExample 2`n`nEnd"

Log_Text := StrReplace(Text, "(`n)+", " ")
The idea is to remove all linefeed `n sequence from a string so that when printed to a log it shows on a single line... The regular expression in the code above do not work...

Help would be appreciated.
User avatar
Datapoint
Posts: 302
Joined: 18 Mar 2018, 17:06

Re: Regular Expression Problem  Topic is solved

11 Dec 2023, 03:37

StrReplace is for plain text, try RegExReplace instead.
User avatar
andymbody
Posts: 943
Joined: 02 Jul 2017, 23:47

Re: Regular Expression Problem

11 Dec 2023, 06:24

Datapoint wrote:
11 Dec 2023, 03:37
try RegExReplace instead.
Just in case the vertical whitespace contains a character other than just LF (`n)

Code: Select all

Text := "This is a test to see`nhow removing splitted lines using`nregular expression looks like when written to a log:`n`nExample 1`n`n`nExample 2`n`nEnd"
Log_Text := RegExReplace(Text, "\R+", " ")
MsgBox % "[" . Log_Text . "]"
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Regular Expression Problem

11 Dec 2023, 13:45

Keep it simple:

Code: Select all

Log_Text := StrReplace(Text, "`n", " ")
Click the word StrReplace in the code box to read more about it in the AHK Documentation.
User avatar
andymbody
Posts: 943
Joined: 02 Jul 2017, 23:47

Re: Regular Expression Problem

11 Dec 2023, 14:49

Xtra wrote:
11 Dec 2023, 13:45
Keep it simple:
Wouldn't this cause multiple spaces where there are multiple LF? I'm not at my computer, so unable to test it.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Regular Expression Problem

11 Dec 2023, 16:13

andymbody wrote:
11 Dec 2023, 14:49
Xtra wrote:
11 Dec 2023, 13:45
Keep it simple:
Wouldn't this cause multiple spaces where there are multiple LF? I'm not at my computer, so unable to test it.
I scrolled to the far right and see them in the example now. Yes it would. You could StrReplace() the 2 spaces into one after but then might as well use RegexReplace(). It would be about the same speed.
Galenmacil
Posts: 2
Joined: 06 Aug 2016, 19:32

Re: Regular Expression Problem

11 Dec 2023, 20:01

Oh boy... I made (kind of) a fool of myself: Yes "StrReplace()" is for plain text string replacement :crazy:. What I needed, as Datapoint succinctly shows, was

Code: Select all

RegExReplace(Text, "(`n)+", " ")
As Xtra also said "Keep it simple.": So Initially the function used was

Code: Select all

StrReplace(Text, "`n", " ")
but this would add unneeded extra space if, like in my example, a string contains many succesive 'n...


I was trying to fix this small part of my script for like 3 hours! I guess at one point your brain can't see clearly.

Back in "the old days" (in the early 90's) when I was coding in plain C (not C++) I would get stuck on a "problem" where the solution was always really obvious and simple, like here. Many times it happened to me... And many times I said to myself "I got to remember that the devil is in the details!"

So, yeah: The goblins are in the details indeed. ;)

Thanks for anyone submitting replies here. :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: DaveF, JoeWinograd, peter_ahk and 201 guests