如何转义正则表达式中的元字符

时间:2022-06-13 00:18:18

Language: asp

This is 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 response will display:

响应将显示:

www.example.com/goaspx?id=1234

I know (.) is one of Meta Character. But what if I want (.) just is (.), not any single word. What should I do?

我知道(。)是元字符之一。但是,如果我想(。)只是(。),而不是任何一个单词。我该怎么办?

Thanks for helping!

谢谢你的帮助!

5 个解决方案

#1


Since . is a metacharacter, to match '.' you need to escape it, like as \.

自从。是一个元字符,匹配'。'你需要逃脱它,就像\一样。

#2


In addition to escaping the . with \, many people like to use a character class with only . in it: [.], they find it more aesthetically pleasing. You also don't run into the problem of multiple levels of escaping. With \, you may have to use many levels of escaping if your language's version of strings treat \ as a special character: "\\.".

除了逃避。用\,很多人喜欢只使用一个字符类。在它:[。],他们发现它更美观。您也不会遇到多级转义的问题。使用\,如果您的语言版本的字符串将\视为特殊字符:“\\。”,则可能必须使用多级转义。

#3


Use \. to match a . literally.

使用 \。匹配一个。从字面上。

#4


You need to escape the meta-characters that you want to be treated literally. In most regex systems this means prefixing it with a backslash. eg: "foo\.bar"

你需要逃避你想要从字面上对待的元字符。在大多数正则表达式系统中,这意味着在其前面添加反斜杠。例如:“foo \ .bar”

#5


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

有关优秀的正则表达式教程,请参阅http://www.regular-expressions.info/

#1


Since . is a metacharacter, to match '.' you need to escape it, like as \.

自从。是一个元字符,匹配'。'你需要逃脱它,就像\一样。

#2


In addition to escaping the . with \, many people like to use a character class with only . in it: [.], they find it more aesthetically pleasing. You also don't run into the problem of multiple levels of escaping. With \, you may have to use many levels of escaping if your language's version of strings treat \ as a special character: "\\.".

除了逃避。用\,很多人喜欢只使用一个字符类。在它:[。],他们发现它更美观。您也不会遇到多级转义的问题。使用\,如果您的语言版本的字符串将\视为特殊字符:“\\。”,则可能必须使用多级转义。

#3


Use \. to match a . literally.

使用 \。匹配一个。从字面上。

#4


You need to escape the meta-characters that you want to be treated literally. In most regex systems this means prefixing it with a backslash. eg: "foo\.bar"

你需要逃避你想要从字面上对待的元字符。在大多数正则表达式系统中,这意味着在其前面添加反斜杠。例如:“foo \ .bar”

#5


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

有关优秀的正则表达式教程,请参阅http://www.regular-expressions.info/