Page 1 of 1

Question about RegExMatch()

Posted: 04 May 2019, 09:58
by Maxsteinfeld
Hi @ all
is it possible to use a var in NeedleRegEx ?
e.g.

Code: Select all

var = test
str = test some text test or more bla bla
if regexmatch(str,"^%var%\s.*")			; this is my idea 
msgbox % var " found at the beginning of " str
I would like to check if the word at the beginning of str contains the var
but my match does not work :problem:
need help !
greetz
Max

Re: Question about RegExMatch()

Posted: 04 May 2019, 10:04
by just me

Code: Select all

if regexmatch(str, "^" . var . "\s.*")			; this is my idea -> expression syntax

Re: Question about RegExMatch()

Posted: 04 May 2019, 10:43
by Maxsteinfeld
@ just me
wow ! big THX
works fine so far :-)
BUT
a very small (?) problem
if;

Code: Select all

var = [test] 		;var with [ and ]
str = [test] some text test or more bla bla
if regexmatch(str, "^" . var . "\s.*")
msgbox % var " found at the beginning of " str
in this case Regexmatch does not work unfortunately do not find [test]
Do not know why :o

Re: Question about RegExMatch()  Topic is solved

Posted: 04 May 2019, 10:49
by Klarion

Code: Select all

var := "[test]" 	
str := "[test] some text test or more bla bla"
varEscaped := RegExReplace(var, "([\\.*?+()[{|$^-])", "\$1")	
If RegexMatch(str, "^" varEscaped "\s.*")
	MsgBox % var " found at the beginning of " str
1. try not to use OLD style
2. try to use help file
Good Luck

Re: Question about RegExMatch()

Posted: 04 May 2019, 10:51
by Klarion

Re: Question about RegExMatch()

Posted: 04 May 2019, 11:09
by Klarion

Re: Question about RegExMatch()

Posted: 04 May 2019, 12:05
by Odlanir

Code: Select all

var := "\[test\]" 		;var with [ and ]
str := "[test] some text test or more bla bla"
if regexmatch(str, "^" . var . "\s.*")
msgbox % var " found at the beginning of " str

Re: Question about RegExMatch()

Posted: 04 May 2019, 16:14
by Maxsteinfeld
@ just me
@Klarion
@Odlanir
Thx for your help

Re: Question about RegExMatch()

Posted: 05 May 2019, 00:46
by jeeswg
@Klarion: Nice code. Btw AFAIK, - does not need escaping in this case.

Code: Select all

;12 characters that need escaping in RegEx generally: \.*?+[{|()^$
;4 characters that need escaping in a RegEx character class: ^-]\

var := "\ . * ? + [ { | ( ) ^ $"
;varEscaped := RegExReplace(var, "([\\.*?+()[{|$^-])", "\$1") ;before
varEscaped := RegExReplace(var, "([\\.*?+()[{|$^])", "\$1") ;after (- removed)
MsgBox, % varEscaped

Re: Question about RegExMatch()

Posted: 05 May 2019, 01:32
by Klarion
Right, you got a point
When you escaped brackets which embracing it, it losts its special meaning as a range symbol within the character class.
So, you do not have to escape - in this case

Re: Question about RegExMatch()

Posted: 05 May 2019, 01:43
by jeeswg
- Outside of a character class, you do not need to escape - or ]
- Inside a character class, you always need to escape these 3 characters: -]\, if intended literally. And you need to escape this character: ^, if intended literally, but only if it is at the start of the character class.
- Something to check is when you use one of these 2 characters: -\, at the end of a character class.

Re: Question about RegExMatch()

Posted: 05 May 2019, 01:48
by Klarion
Good
Now it is time to make another formal routine of jeeswg called..
escape the escape
Good Luck

Re: Question about RegExMatch()

Posted: 05 May 2019, 02:55
by just me

Code: Select all

Var := "[test]" 		; var with [ and ]
Str := "[test] some text test or more bla bla"
If RegExMatch(str, "^\Q" . var . "\E\s")
   MsgBox, %Var%`nfound at the beginning of`n%Str%
Else
   MsgBox, Did not find`n%Var%`nin`n%Str%
ExitApp
\E must not be part of the needle. (Edit: Replaced haystack with needle)

Re: Question about RegExMatch()

Posted: 05 May 2019, 03:04
by jeeswg
Some ideas here:
simplest way to make a RegEx needle literal? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30420