正则表达式

正则表达式是一种模式匹配语言。利用正则表达式,您可以对文本执行令人惊讶的和复杂的转换。由于正则表达式太复杂,所以不能在这里作完整的解释,请参考下面的这些教程之一:

正则表达式指南

模式匹配与正则表达式

或者是搜寻网址获得更多信息。

共通问题

特殊字符

如果你的正则表达式无效,或则是做一些意料之外的事情,你可以试着用一些特殊字符:

\^$*+?.()|{}[]

如果你在匹配的时候想利用这些匹配字符,你就必须在他们前面加上斜线(\)。所以如果你想匹配 "text.htm?pagewanted",你需要这样写: "text\.htm\?pagewanted"。

除了斜线,在正则表达式的置换部分的任何字符都是普通字符,因此你不需要在他们前面加上斜线。

\^$*+?.()|{}[]

If you want to use one of those characters as part of what you're matching, you need to put a backslash (\) in front of it.  So if you wanted to match "text.htm?pagewanted" you should write "text\.htm\?pagewanted".

Except for the backslash, none of those characters are special in the replacement part of the regular expression, so you don't have to put a backslash in front of them in the replacement part.