Home > Back-end >  regex for "finding whole word only" plus allowing one character
regex for "finding whole word only" plus allowing one character

Time:08-14

Hi I'm using this regular expression to find whole word only: example:

Dim oRE, bMatch
Set oRE = New RegExp
oRE.Pattern = "\bFunction\b"
bMatch = oRE.Test("Functions") 'return false
bMatch = oRE.Test("Function dummy") 'return true

I want to allow one character at the end of the string. The char i want to allow is the double quote ("). So i would like this line of code to return true:

bMatch = oRE.Test("Function" chr(34) " dummy") 'chr(34) is the charcode of doublequote (") 

CodePudding user response:

Initiate a variable with chr(34) and concatenate it into your pattern.

dq = Chr(34)
oRE.Pattern = "\bFunction" & dq & " \b"

Then you will be able to match the double quotes as well.

for 1 or more double quotes after Function (modify it per your needs).

CodePudding user response:

The double quote can be written like this \x22 in order to replace it easily in your pattern "

Hope that this what you want as result Demo here


Dim oRE, bMatch
Set oRE = New RegExp
oRE.Pattern = "\bFunction. ?\x22"
aMatch = oRE.Test("Functions""")
bMatch = oRE.Test("Function dummy""")
wscript.echo "Functions " & aMatch
wscript.echo "Functions dummy " & bMatch
  • Related