How to avoid a metacharacter in a regular expression

Language: asp

This is a sample of my code:

str = "www.example.com/gotobuy.aspx?id=1234"
key_word = ".obuy."
Dim regEx
Set regEx = New RegExp
regEx.Pattern = key_word
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(str)
if matches.count > 0 then
    new_string =  str
    For Each Match in Matches
        new_string = replace(new_string,match.value,"")
    Next
else
    new_string = str
end if
response.write new_string

      

The answer will open:

www.example.com/goaspx?id=1234

      

I know (.) Is one of the metacharacters. But what if I want (.) Just (.), Not a single word. What should I do?

Thanks for the help!

0


a source to share


5 answers


Because. is a metacharacter to match "." you need to escape from it, for example\.



+2


a source


In addition to the screening of .

a \

lot of people like to use a character class only .

in it: [.]

they think it more aesthetically pleasing. You also don't run into the issue of multiple levels of escaping. Since \

you may need to use a lot of levels of screening, if your language version of the line is treated \

as a special character: "\\."

.



+3


a source


Use literal \.

to match .

.

+2


a source


You need to avoid the metacharacters that you want to literally process. In most regex systems, this means a backslash prefix. eg:"foo\.bar"

+1


a source


For a great regex tutorial see http://www.regular-expressions.info/

0


a source







All Articles