Tutorial :How to escape a meta character in a regular expression


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:


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!


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


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: "\\.".


Use \. to match a . literally.


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"


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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »