how to make regexmatch only search up to a certain character or position and ignore the rest of the text Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rj8810
Posts: 31
Joined: 16 Jul 2018, 22:34

how to make regexmatch only search up to a certain character or position and ignore the rest of the text

Post by rj8810 » 17 Aug 2022, 10:32

hello beautiful auothotkey community, I have the following code:

Code: Select all

q::
str := "yellow3xxxyellow2xxxyellow1xxxbluexxxred1xxxred2xxxred3xxxyellow....against...yellow2xxxblue2xxxred2...againt...yellow3xxxblue3xxxred3"

p := 1
array := []
while p:= RegExMatch(str, "yellow(?:(?!yellow).)*?blue.*?red", m, p+StrLen(m))
{
Array[A_Index] := m ;esto sólo recupera el subpatron
msgbox % "Element number " . A_Index . " is " . Array[A_Index]
Count := Array.Count()
}
msgbox, % "count =" Count
return

;msgbox, % "count =" Count
Z::
ExitApp
but I was wondering if there is a way to make regexmatch only search up until the word again, and ignore the rest of the text, I know that in the autohotkey documentation there is the StartingPos option, but ¿is there the option endpos or finishpos or untilpos? in rexexmatch

Thanks in advance:
note: I would not like to use InStr() and SubStr(), I would really like just one line of code with regexmatch if possible.
Last edited by rj8810 on 19 Aug 2022, 23:30, edited 2 times in total.

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

Re: how to make regexmatch only search up to a certain character and ignore the rest of the text

Post by mikeyww » 17 Aug 2022, 11:39

Code: Select all

str  = axabcxdebfxghibxjklb
find = b
RegExMatch(str, "P).*?x.*?(\Q" find "\E).*?x", m)
MsgBox, 64, Position, %mPos1%

rj8810
Posts: 31
Joined: 16 Jul 2018, 22:34

Re: how to make regexmatch only search up to a certain character and ignore the rest of the text

Post by rj8810 » 18 Aug 2022, 21:42

apparently the use of .*?keywordendposition seems to be the answer, or the use of look behind a look ahead assertions, but 2 lines of regexmatch code are needed, one to capture the substring of the entire string and separate it from it, and the other to search in the substring if the desired words exist

rj8810
Posts: 31
Joined: 16 Jul 2018, 22:34

Re: how to make regexmatch only search up to a certain character and ignore the rest of the text

Post by rj8810 » 18 Aug 2022, 23:03

mikeyww wrote:
17 Aug 2022, 11:39

Code: Select all

str  = axabcxdebfxghibxjklb
find = b
RegExMatch(str, "P).*?x.*?(\Q" find "\E).*?x", m)
MsgBox, 64, Position, %mPos1%
THANK YOU VERY MUCH "mikeyww", IT SEEMS THAT THE DELIMITING CHARACTER HERE is the "X",This worked fine for me when I want to put a known character as a delimiter, but how would I achieve this when the character is unknown but I have the number of the position. I was wondering if it is possible, instead of a given character, to set as delimiter (endposition or Length) a given numeric position, in which there is an unknown character, something similar to the substr () function.

Code: Select all

NewStr := SubStr(String, StartingPos , Length)
I would like for example to start at position 5 and end at position 20, and the rest of the text is ignored.

Code: Select all

RegExMatch(Haystack, NeedleRegEx , OutputVar, StartingPos := 5, endposorlenght := 20)
[code]
Thanks in advance

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

Re: how to make regexmatch only search up to a certain character and ignore the rest of the text

Post by mikeyww » 19 Aug 2022, 06:07

You can use a substring as your haystack, or:

Code: Select all

str    = axabcxdebfxghibxjklb
find   = b
start  = 10
end    = 20
pos   := (pos := RegExMatch(str, find,, start)) > end ? 0 : pos
MsgBox, 64, Position, %pos%

rj8810
Posts: 31
Joined: 16 Jul 2018, 22:34

Re: how to make regexmatch only search up to a certain character and ignore the rest of the text  Topic is solved

Post by rj8810 » 19 Aug 2022, 23:28

mikeyww wrote:
19 Aug 2022, 06:07
You can use a substring as your haystack, or:

Code: Select all

str    = axabcxdebfxghibxjklb
find   = b
start  = 10
end    = 20
pos   := (pos := RegExMatch(str, find,, start)) > end ? 0 : pos
MsgBox, 64, Position, %pos%
wow, thank you very much, it works great, I owe you one.
by the way, i renamed the topic for someone in the future has the same question:
"how to make regexmatch only search up to a certain character or position and ignore the rest of the text"
bye

Post Reply

Return to “Ask for Help (v1)”