Showing the first lines

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

Showing the first lines

Post by Billykid » 29 Mar 2024, 08:19

Code: Select all

I := 3
H := "Line_1`r`nLine_2`r`nLine_3`r`nLine_4`r`nLine_5`r`nLine_6`r`nLine_7`r`nLine_8`r`nLine_9"
msgBox % regexreplace(H,"`a).*\R?",,,I)  

This regular expression displays the lines from 4 to 9. How do I achieve the reverse result,
so that rows 1 to 3 are displayed and rows 4 to 9 are not?

Thanks a lot in advance.
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Showing the first lines

Post by Rohwedder » 29 Mar 2024, 10:00

Hallo,
try:

Code: Select all

I := 3
H := "Line_1`r`nLine_2`r`nLine_3`r`nLine_4`r`nLine_5`r`nLine_6`r`nLine_7`r`nLine_8`r`nLine_9"
RegExMatch(H, "(.*\R*){" I "}", M), M := RTrim(M,"`r`n")
MsgBox,% M
User avatar
Chunjee
Posts: 1422
Joined: 18 Apr 2014, 19:05
Contact:

Re: Showing the first lines

Post by Chunjee » 29 Mar 2024, 11:41

just for fun:

Code: Select all

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

I := 3
H := "Line_1`r`nLine_2`r`nLine_3`r`nLine_4`r`nLine_5`r`nLine_6`r`nLine_7`r`nLine_8`r`nLine_9"

firstThreeLines := A.join(A.take(A.split(H, "`r`n"), I), "`n")
msgbox, % firstThreeLines
; => "Line_1`nLine_2`nLine_3"
.take Creates a slice of array with n elements taken from the beginning. Three in this case

https://biga-ahk.github.io/biga.ahk/#/?id=take
https://biga-ahk.github.io/biga.ahk/#/?id=split
https://biga-ahk.github.io/biga.ahk/#/?id=join
Post Reply

Return to “Ask for Help (v1)”