Question about RegExMatch() Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

Question about RegExMatch()

04 May 2019, 09:58

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
just me
Posts: 9425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Question about RegExMatch()

04 May 2019, 10:04

Code: Select all

if regexmatch(str, "^" . var . "\s.*")			; this is my idea -> expression syntax
Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

Re: Question about RegExMatch()

04 May 2019, 10:43

@ 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
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Question about RegExMatch()  Topic is solved

04 May 2019, 10:49

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
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Question about RegExMatch()

04 May 2019, 12:05

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
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

Re: Question about RegExMatch()

04 May 2019, 16:14

@ just me
@Klarion
@Odlanir
Thx for your help
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Question about RegExMatch()

05 May 2019, 00:46

@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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Question about RegExMatch()

05 May 2019, 01:32

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
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Question about RegExMatch()

05 May 2019, 01:43

- 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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Question about RegExMatch()

05 May 2019, 01:48

Good
Now it is time to make another formal routine of jeeswg called..
escape the escape
Good Luck
just me
Posts: 9425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Question about RegExMatch()

05 May 2019, 02:55

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)
Last edited by just me on 05 May 2019, 03:54, edited 1 time in total.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Question about RegExMatch()

05 May 2019, 03:04

Some ideas here:
simplest way to make a RegEx needle literal? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30420
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Billykid and 168 guests