Find the first occurrence of text and stop
Find the first occurrence of text and stop
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 .
-
- Posts: 16
- Joined: 16 Mar 2019, 12:30
Re: Find the first occurrence of text and stop
Hi,
so if there's a "|-" in the text you want it to stop and perform some other action?
i guess this might help.
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
Re: Find the first occurrence of text and stop
electrone77, much much thanks for the code.
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
-
- Posts: 16
- Joined: 16 Mar 2019, 12:30
Re: Find the first occurrence of text and stop
yw i'm glad it worked
- Blue Kodiak
- Posts: 26
- Joined: 17 Mar 2019, 00:45
Re: Find the first occurrence of text and stop
To get only the first segment before the first |
Remove the -1 to include the | in the segment
To get the first and each subsequent segment between successive |s
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
EDITED: March 19, 2019 - Corrected error in second code block - SegmentTally assignment erroneously placed line inside the While loop instead of before it.
Code: Select all
Segment := SubStr(TextBlockVar, 1 , (j := InStr(TextBlockVar, "|", False, 1)-1) )
;Do those "other actions"
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?
}
}
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.
Re: Find the first occurrence of text and stop
Blue Kodiak thanks for the help.
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
-
- Posts: 16
- Joined: 16 Mar 2019, 12:30
Re: Find the first occurrence of text and stop
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:
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.
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.
}
- RegEx , RegExReplace , RegExMatch
have a nice day.
Re: Find the first occurrence of text and stop
electrone77, thanks again and your English is excellent
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
Re: Find the first occurrence of text and stop
If you want to search for static text like |-, use InStr(). You don't need regular expressions wasting resources in this case.
Who is online
Users browsing this forum: marypoppins_1 and 133 guests