Select a piece of text from a TXT file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Guill
Posts: 141
Joined: 09 Jun 2016, 22:00

Select a piece of text from a TXT file

16 Apr 2020, 21:31

Hello guys

I need the following:

I want that AHK selects a segment from a certain start, to a certain end, of a TXT file in which I have certain email addresses, and which, at the same time, I modify frequently,
And after selecting it, copy it to the Clipboard.

The FileReadLine command doesn't work for me because it's just for lines.

I know that it exists FileOpen and FileRead, but I cannot do it by myself.

How could I do it?

Example:

File: "C: \ Documents \ Adress.txt"

Start: ahd@ces.com
End: hke@hdc.com
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Select a piece of text from a TXT file  Topic is solved

16 Apr 2020, 23:01

Code: Select all

Text =
(
Here is some random text, and this is
the start email address ahd@ces.com which
is followed by some more random text
before finally reaching the ending
email address hke@hdc.com followed
by some more random text.
)

; un-comment the following line and remove the demo-only lines above
; FileRead, Text, C:\Documents\Adress.txt ; note: "Adress" is how it was spelled in your post

RegExMatch(Text, "s)ahd@ces\.com.*hke@hdc\.com", Match)
Clipboard := Match
MsgBox, The selected text is in the clipboard
Guill
Posts: 141
Joined: 09 Jun 2016, 22:00

Re: Select a piece of text from a TXT file

17 Apr 2020, 07:46

Thank you very mucth Boiler!

Very clear and didactic!
garry
Posts: 3852
Joined: 22 Dec 2013, 12:50

Re: Select a piece of text from a TXT file

17 Apr 2020, 11:50

@boiler, thank you , very short with regexmatch
also a script with function xStr() from SKAN
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=74050

Code: Select all

/*
xStr( H, C, B ,E, BO, EO, BI, EI, BT, ET )
1 H = Haystack
2 C = Case sensitivity (for B,E)
3 B = Begin match
4 E = End match
5 BO = Begin offset
6 EO = End offet
7 BI = Begin instance
8 EI = End instance
9 BT = Begin (un)trim
10ET = End (un)trim
*/

H =
(
Here is some random text, and this is
the start email address ahd@ces.com which
is followed by some more random text
before finally reaching the ending
email address hke@hdc.com followed
by some more random text.
)
A= ahd@ces.com
B= hke@hdc.com
c:=xStr(H,,A,B,,,,,0,0)
d:=xStr(H,,A,B)           ;- between A and B
msgbox,%c%`n--------------------`n%d%
return
;----------------------------------------------------------------
;- SKAN  xStr  for general text extraction and parsing XML  HTML 
;- https://www.autohotkey.com/boards/viewtopic.php?f=6&t=74050
xStr(ByRef H, C:=0, B:="", E:="",ByRef BO:=1, EO:="", BI:=1, EI:=1, BT:="", ET:="") {                           
Local L, LB, LE, P1, P2, Q, N:="", F:=0                 ; xStr v0.97 by SKAN on D1AL/D343 @ tiny.cc/xstr  
Return SubStr(H,!(ErrorLevel:=!((P1:=(L:=StrLen(H))?(LB:=StrLen(B))?(F:=InStr(H,B,C&1,BO,BI))?F+(BT=N?LB
:BT):0:(Q:=(BO=1&&BT>0?BT+1:BO>0?BO:L+BO))>1?Q:1:0)&&(P2:=P1?(LE:=StrLen(E))?(F:=InStr(H,E,C>>1,EO=N?(F
?F+LB:P1):EO,EI))?F+LE-(ET=N?LE:ET):0:EO=N?(ET>0?L-ET+1:L+1):P1+EO:0)>=P1))?P1:L+1,(BO:=Min(P2,L+1))-P1)  
}
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Select a piece of text from a TXT file

17 Apr 2020, 11:55

@garry, nice option to have.

By the way, to do the "between" approach in the RegEx version:

Code: Select all

Text =
(
Here is some random text, and this is
the start email address ahd@ces.com which
is followed by some more random text
before finally reaching the ending
email address hke@hdc.com followed
by some more random text.
)

; un-comment the following line and remove the demo-only lines above
; FileRead, Text, C:\Documents\Adress.txt ; note: "Adress" is how it was spelled in your post

RegExMatch(Text, "s)(?<=ahd@ces\.com).*(?=hke@hdc\.com)", Match)
Clipboard := Match
MsgBox, The selected text is in the clipboard
garry
Posts: 3852
Joined: 22 Dec 2013, 12:50

Re: Select a piece of text from a TXT file

17 Apr 2020, 12:01

@boiler, thank you for the example , I knew you can do everything with regex ( which I can't )
example 'between' I often used

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo and 140 guests