help with string extract please Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wer
Posts: 57
Joined: 29 Nov 2022, 21:28

help with string extract please

Post by wer » 31 Jan 2023, 00:09

have some txt files, the content like this:
1. xxx
xxx
2. xxx
xxx
3. xxx
xxx
4. xxx
xxx
5. xxx
xxx
6. xxx
xxx
7. xxx
xxx
……
is it possible to extract the last number (7. in this case) of the content
(not sure which line will be the last line contains the number)
(there are some blanks before and after the number actually)
thanks in advance

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: help with string extract please  Topic is solved

Post by mikeyww » 31 Jan 2023, 00:18

Code: Select all

#Requires AutoHotkey v1.1.33
str =
(
1. xxx
xxx
2. xxx
xxx
3. xxx
xxx
4. xxx
xxx
5. xxx
xxx
6. xxx
xxx
709. xxx
xxx
……
)
MsgBox 64, Last number, % lastNumber(str)

lastNumber(str) {
 RegExMatch("`n" str, ".*\n\K\d+", m)
 Return m
}

Code: Select all

#Requires AutoHotkey v2.0
str := "
(
1. xxx
xxx
2. xxx
xxx
3. xxx
xxx
4. xxx
xxx
5. xxx
xxx
6. xxx
xxx
709. xxx
xxx
……
)"
MsgBox lastNumber(str), 'Last number', 64

lastNumber(str) {
 RegExMatch("`n" str, "s).*\n\K\d+", &m)
 Return m[]
}

Code: Select all

#Requires AutoHotkey v2.0
lastNumber := (str) => RegExReplace("`n" str, "s).*\n(\d+).*", "$1")
str := "
(
1. xxx
xxx
2. xxx
xxx
3. xxx
xxx
4. xxx
xxx
5. xxx
xxx
6. xxx
xxx
709. xxx
xxx
……
)"
MsgBox lastNumber(str), 'Last number', 64

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: help with string extract please

Post by wer » 31 Jan 2023, 06:15

@mikeyww
right, thanks a lot as always dear sir mikeyww
thanks for showing me different methods to achieve this
i can not mastered the v2 by now, with you kindly help,
actually in my case, it should be like, with v1:

Code: Select all

RegExMatch("`n" str, ".*\n\s+\K\d+\.", m)
you truly regex master

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: help with string extract please

Post by DuckingQuack » 31 Jan 2023, 06:30

:shock: I almost never do anything outside of keystrokes and timers and that line of code makes me scared to venture outside… I’ll stay in my lane for now :shock:
I’m going to have to figure this one out later, but thanks for a wonderful example to use for learning.
Best of Luck,
The Duck

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: help with string extract please

Post by mikeyww » 31 Jan 2023, 07:24

When time permits, you can read the documentation page that introduces regex.

Code: Select all

s).*\n(\d+).*
s) causes a period (.) to match all characters including newlines

. is any single character

* is zero or more of the preceding character, as many times as possible, giving back as needed

\n is line feed

\d is a digit

+ is one or more of the preceding character, as many times as possible, giving back as needed

() captures the matching subpattern into a backreference

The result is like: (optional stuff)(line feed)(some digits)(optional stuff)

In RegExReplace, $1 then refers back to the matching subpattern.

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: help with string extract please

Post by wer » 01 Feb 2023, 00:25

@mikeyww
right, you are a good teacher, sir
dont know why, when i read txt file to str, then use regexmatch on this str,
it always return the first not last number
finally, i have to use loop read line to solve it:

Code: Select all

Loop, read, %folder_path%\%A_LoopFileName%
{
   if RegExMatch(A_LoopReadLine, "\s\s\K\d+\.", curr)!=0)
   last:=curr
}
msgbox, %last%

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: help with string extract please

Post by mikeyww » 01 Feb 2023, 00:50

The default "newline" is `r`n, but . does not match newlines by default.
By default, a dot matches any single character which is not part of a newline (`r`n) sequence, but this can be changed by using the DotAll (s), linefeed (`n), carriage return (`r), `a or (*ANYCRLF) options.
As the text file contains newlines (unlike the scripts posted earlier), the "dot all" option solves the problem.

Code: Select all

; This script returns the last leading number in a text file
#Requires AutoHotkey v1.1.33
FileRead str, % A_ScriptDir "\test.txt"
MsgBox 64, Last number, % lastNumber(str)

lastNumber(str) {                            ; Get the last leading number
 RegExMatch("`n" str, "s).*\n\h*(\d+)\.", m) ; Ignore leading space; number must end in dot
 Return m1                                   ; Return only the digits
}

Tested:

Code: Select all

1. xxx
xxx
2. xxx
xxx
3. xxx
xxx
4. xxx
xxx
5. xxx
xxx
6. xxx
xxx
 709. xxx
 831
xxx
image230201-0644-001.png
Output
image230201-0644-001.png (3.88 KiB) Viewed 298 times
Attachments
test.txt
Test file
(98 Bytes) Downloaded 8 times

Post Reply

Return to “Ask for Help (v1)”