regexmatch match any line exactly in list problem Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
pcb
Posts: 5
Joined: 07 Apr 2018, 07:58

regexmatch match any line exactly in list problem

07 Apr 2018, 08:19

Hi,
I have read list.txt as a variable called mylist

list.txt contained these lines
yes-4k
4k
a4k
sk-4k
no.4k
4k-3

Then I had an input box asking me to check to see if a line exists - var
I'm trying to use regexmatch to do this.

pos := RegExMatch(mylist, var , SubPat)

Then I need to check if it exists without looping through each line
If (var = subpat1)
msgbox, yes
Else
msgbox, no

Some No results would be when var = 4k- or s-4k or sk-4

I have experimented with borders and whitespace, /n , $ and ^ etc but no joy.
swagfag
Posts: 2829
Joined: 11 Jan 2017, 17:59

Re: regexmatch match any line exactly in list problem

07 Apr 2018, 10:04

its not clear what you want

post a snippet of or your full code and explain whats wrong with it
pcb
Posts: 5
Joined: 07 Apr 2018, 07:58

Re: regexmatch match any line exactly in list problem

07 Apr 2018, 10:27

Hi,
I want to be able to type in one of those lines that are in list.txt to an input box then adjust list.txt so that it removes the exact line I entered.


FileRead, mylist, list.txt
inputbox, var, word to match, input word
myfind := "\b(" . var. "\b)"

pos := RegExMatch(mylist, myfind , subpat)

If (match = subpat)
msgbox, found input

mylist := regexreplace(mylist, myfind,"`n")
msgbox, %mylist%


If I type in 4k, I just want it to remove the line with 4k in it. It's removing the one's next to boundaries which I have learned includes non word characters.
Odlanir
Posts: 470
Joined: 20 Oct 2016, 08:20

Re: regexmatch match any line exactly in list problem

07 Apr 2018, 13:04

Code: Select all

list=
(
yes-4k
4k
a4k
sk-4k
no.4k
4k-3
)
lineToRemove := "4k" ; change to remove the line you desire
list := RegExReplace(list, "m`a)^" lineToRemove "$\r*\n*")
MsgBox % list
ExitApp
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
pcb
Posts: 5
Joined: 07 Apr 2018, 07:58

Re: regexmatch match any line exactly in list problem

07 Apr 2018, 14:38

Thanks Odlanir. This didn't work for me if I typed in ...4k
My list can contain entries with a-zA-Z0-9_\-\. in all kinds of crazy orders

...4k removed sk-4k and no.4k

I can acheive what I need with a loop but with 1000's of entries, i'm looking for efficiency.
User avatar
Cuadrix
Posts: 199
Joined: 07 May 2017, 08:26

Re: regexmatch match any line exactly in list problem

07 Apr 2018, 16:34

pcb wrote:Thanks Odlanir. This didn't work for me if I typed in ...4k
My list can contain entries with a-zA-Z0-9_\-\. in all kinds of crazy orders

...4k removed sk-4k and no.4k

I can acheive what I need with a loop but with 1000's of entries, i'm looking for efficiency.
What do you mean? The code Odlanir posted, works perfectly.
Just replace lineToRemove with an InputBox:

Code: Select all

list=
(
yes-4k
4k
a4k
sk-4k
no.4k
4k-3
)

InputBox, Input , String! -> Delete line, Type the contents of the line that you want to delete:,, 200, 150
list := RegExReplace(list, "m`a)^" Input "$\r*\n*")
MsgBox % list
----
pcb
Posts: 5
Joined: 07 Apr 2018, 07:58

Re: regexmatch match any line exactly in list problem

07 Apr 2018, 21:40

Cuadrix wrote: What do you mean? The code Odlanir posted, works perfectly.
Not for me.
What happens if you add anther string to the list, eg ...4k

Then you enter ...4k as the input to remove the line. 3 entries disappear.
Odlanir
Posts: 470
Joined: 20 Oct 2016, 08:20

Re: regexmatch match any line exactly in list problem  Topic is solved

08 Apr 2018, 03:23

It works if you prefix all the regex special chars in haystack with backslash.

Code: Select all

list := RegExReplace(list, "m`a)^" RegExReplace(input, "[-[\]{}()*+?.,\\/^$|#]", "\$0") "$\R*")
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
pcb
Posts: 5
Joined: 07 Apr 2018, 07:58

Re: regexmatch match any line exactly in list problem

08 Apr 2018, 10:52

Thank you Odlanir. Regexpert level 1 million!

Return to “Ask For Help”

Who is online

Users browsing this forum: Bad husband, Bing [Bot], flyingDman and 185 guests