Find the first occurrence of text and stop

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Find the first occurrence of text and stop

16 Mar 2019, 13:25

I have a page of text in which contains numerous "|-". How is it possible to find a single occurrence and then stop so that the macro can perform some other action?
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
electrone77
Posts: 16
Joined: 16 Mar 2019, 12:30

Re: Find the first occurrence of text and stop

16 Mar 2019, 15:18

Hi,
so if there's a "|-" in the text you want it to stop and perform some other action?
i guess this might help.

Code: Select all

text = I have a page of text in which contains numerous "|-". How is it possible to find a single occurrence and then stop so that the macro can perform some other action?
 	if RegExMatch(text, "\|\-")
	;perform some other action
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Find the first occurrence of text and stop

16 Mar 2019, 16:32

electrone77, much much thanks for the code.
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
User avatar
Blue Kodiak
Posts: 26
Joined: 17 Mar 2019, 00:45

Re: Find the first occurrence of text and stop

17 Mar 2019, 03:30

To get only the first segment before the first |

Code: Select all

Segment := SubStr(TextBlockVar, 1 , (j := InStr(TextBlockVar, "|", False, 1)-1) )
;Do those "other actions"
Remove the -1 to include the | in the segment

To get the first and each subsequent segment between successive |s

Code: Select all

i := 1
j := 1
SegmentTally := 0  ; Erroneous had this assignment inside the loop in my original post. 
While (j)
{
  j := InStr(TextBlockVar, "|", False, i)
  Segment := j ? SubStr(TextBlockVar , i, j-i) : SubStr(TextBlockVar, i)
  If (j) OR (Segment) 
  {
    i := j + 1
    SegmentTally++ 
    ; Do those "other actions" - on the segment maybe?
  }
}
Change the -i to -i+1 to include the | in each segment
Remove the "(j) OR" if the "other actions" aren't performed when the extracted segment is an empty string.

Or more simply use Loop Parse

Code: Select all

Loop , Parse , TextBlockVar, |
{
  Segment := A_LoopField
  SegmentTally := A_Index
  ; Do those "other actions" - on the segment maybe?
}
The | cannot be included in each segment this way, but do you want it?

EDITED: March 19, 2019 - Corrected error in second code block - SegmentTally assignment erroneously placed line inside the While loop instead of before it.
Last edited by Blue Kodiak on 19 Mar 2019, 06:44, edited 1 time in total.
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Find the first occurrence of text and stop

17 Mar 2019, 16:24

Blue Kodiak thanks for the help.
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
electrone77
Posts: 16
Joined: 16 Mar 2019, 12:30

Re: Find the first occurrence of text and stop

18 Mar 2019, 16:35

in-case i might have misunderstood your question, please excuse my poor English.
in addition to Blue Kodiak's post (which is awesome), here is another way to see if there are more than 1 occurrances in the text:

Code: Select all

text = your text here
if regexmatch(text, "(\|\-.*?){2}")	;so {2} here is: number of occurrances, you can edit it according to how many occurrance you want to check.
{
;perform some other action.. you can add [loop, parse] here if you like.
}
i'm not sure if you want to edit the text after the "|-", you might further want to look into:
- RegEx , RegExReplace , RegExMatch

have a nice day.
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Find the first occurrence of text and stop

18 Mar 2019, 19:23

electrone77, thanks again and your English is excellent
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
just me
Posts: 9461
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Find the first occurrence of text and stop

19 Mar 2019, 04:20

If you want to search for static text like |-, use InStr(). You don't need regular expressions wasting resources in this case.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 183 guests