Page 1 of 1

regexmatch match any line exactly in list problem

Posted: 07 Apr 2018, 08:19
by pcb
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.

Re: regexmatch match any line exactly in list problem

Posted: 07 Apr 2018, 10:04
by swagfag
its not clear what you want

post a snippet of or your full code and explain whats wrong with it

Re: regexmatch match any line exactly in list problem

Posted: 07 Apr 2018, 10:27
by pcb
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.

Re: regexmatch match any line exactly in list problem

Posted: 07 Apr 2018, 13:04
by Odlanir

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

Re: regexmatch match any line exactly in list problem

Posted: 07 Apr 2018, 14:38
by pcb
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.

Re: regexmatch match any line exactly in list problem

Posted: 07 Apr 2018, 16:34
by Cuadrix
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

Re: regexmatch match any line exactly in list problem

Posted: 07 Apr 2018, 21:40
by pcb
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.

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

Posted: 08 Apr 2018, 03:23
by Odlanir
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*")

Re: regexmatch match any line exactly in list problem

Posted: 08 Apr 2018, 10:52
by pcb
Thank you Odlanir. Regexpert level 1 million!