Regex to check if keyword is found in a string and keyword should be an exact match

Hi,

I’m trying to use Regex to check if keyword is found in a string and should be an exact match. Currently, I have tried to use boundary, \b[keyword]\b but it accepts special characters. I need exact matching for alphanumeric only.

sample:

Keyword = test

Expected Output:
This is a test //found
This is a -test+ //not found
This is a justatest for demo-purposes //not found
This is a test if regex is working //found
Test is working //found

Thanks

@mnlatam

You can use this

[A-Za-z0-9 ]*

Cheers

You could try this regex:

(?<=^| )Test(?=$| )

You will also need the regex options multiline and ignorecase for the expression to work.

Hi @ptrobot , forgot to mention that I’m using this regex line inside a macro text file and using Invoke VBA. Seems like positive lookbehind is not working for VBA, are you able to confirm? Thanks

1 Like

No, it doesn’t seem like VBA regex supports lookbehind at all. But you can remove it and still get the same result (i.e. if the key word exists or not.)

(^| )Test(?=$| )

Line start or space - CaseInsensitive Keyword - Line end or space

Also rewritten to no anchors ready to trim it later:
grafik

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.