1. 程式人生 > 其它 >golang 正則 反斜槓_Python Re:正則表示式

golang 正則 反斜槓_Python Re:正則表示式

技術標籤:golang 正則 反斜槓

特殊字元:

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5E%7D%7D%7D :匹配字串的開始

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%24%7D%7D%7D :匹配字串的結束

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B.%7D%7D%7D :匹配除卻 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cn%7D%7D%7D 之外的一切字元

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C%7D%7D%7D :轉義字元

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7BA%7CB%7D%7D%7D :A 或者 B,非貪心匹配,順序從左往右

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%2B%7D%7D%7D :一次或者多次,貪心匹配

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%2A%7D%7D%7D :任意次數,貪心匹配

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%3F%7D%7D%7D :零次或者一次,貪心匹配。如果出現在 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%3F%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%2B%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%2A%7D%7D%7D 之後表示非貪心匹配

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%7Bm%5C%7D%7D%7D%7D :m 次

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%7Bm%2Cn%5C%7D%7D%7D%7D :m 到 n 次,貪心匹配

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%7Bm%2Cn%5C%7D%3F%7D%7D%7D :非貪心匹配

特殊字元:

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5Cw%7D%7D%7D :包括大小寫英文字母,數字和下劃線

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5CW%7D%7D%7D :除卻 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cw%7D%7D%7D 之外的所有字元

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5Cd%7D%7D%7D :0-9 數字

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5CD%7D%7D%7D :除卻 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cd%7D%7D%7D 之外的所有字元

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cs%7D%7D%7D :空白字元:包括空格, equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Ct%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cn%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cr%7D%7D%7D

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5CS%7D%7D%7D :非空白字元

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cb%7D%7D%7D :匹配單詞邊界,也就是說,是 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cw%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5CW%7D%7D%7D 之間。注意它是不對應字元的

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5CB%7D%7D%7D :匹配非單詞邊界,也就是說,是 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cw%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cw%7D%7D%7D 之間或者 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5CW%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5CW%7D%7D%7D 之間。注意它是不對應字元的

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5CA%7D%7D%7D :字串的開始

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5CZ%7D%7D%7D :字串的結束

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cn%7D%7D%7D :換行

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Ct%7D%7D%7D :製表符

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cr%7D%7D%7D :回車

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C%22%7D%7D%7D :一切標點符號都能構成特殊字元,表示它原本的意思。也就是說 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C%5C%24%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%24%7D%7D%7D 在正則表示式中都代表 equation?tex=%5Ctexttt%7B%5C%24%7D ;但是 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C%5C%3F%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%3F%7D%7D%7D 在正則表示式中前者代表問號,後者代表一個或者多個。

其餘還有

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5Ca%7D%7D%7Dequation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5Cf%7D%7D%7Dequation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5Cv%7D%7D%7D

集合:

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5B%5D%7D%7D%7D 中的內容表示集合,注意並不是代表方括號。
  • 集合只匹配一個字元,集合中的元素是並列的,比如 equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5Babc%5D%7D%7D%7D 匹配 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Ba%7D%7D%7D 或者 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Bb%7D%7D%7D 或者 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Bc%7D%7D%7D
  • 區間用 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B-%7D%7D%7D 表示,比如 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5Ba-z%5D%7D%7D%7D 會匹配所有小寫字母。如果要表示字面意義上的 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B-%7D%7D%7D ,要麼它前面加上 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C%7D%7D%7D ,要麼它位於第一個或者最後一個: equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5Ba%5C%5C-z%5D%7D%7D%7D 或者 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5B-az%5D%7D%7D%7D 或者 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5Baz-%5D%7D%7D%7D
  • 除卻 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C%7D%7D%7D 之外的特殊字元(除例外 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5D%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B-%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5E%7D%7D%7D )失去它們原有的意義,只表示本來的字面字元;
  • 轉義字元依然有效;
  • equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5E%7D%7D%7D 如果位於第一個位置表示除卻後面字元的所有字元,如果不在第一個位置就是字面含義;
  • 在集合中如果要表示字面意義上的 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5D%7D%7D%7D 需要使用 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C%5D%7D%7D%7D ,或者將其置於第一個位置。

組:

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%28%29%7D%7D%7D :中的內容表示一個組。如果要匹配字面意義上的圓括號,使用 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C%28%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C%29%7D%7D%7D 或者 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5B%28%5D%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5B%29%5D%7D%7D%7D ,在之後可以用 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C1%7D%7D%7D%2C+%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C2%7D%7D%7D%2C+... 表示第一個組,第二個組的內容。

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%28%3FaiLmsux%29%7D%7D%7D :並不匹配字元,表示 equation?tex=%5Ctexttt%7Bflag%7D ,比如 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%28%3Fi%29%7D%7D%7D 表示忽略大小寫匹配。

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%28%3FP%3Cname%3E...%29%7D%7D%7D :表示一個名字叫做 equation?tex=%5Ctexttt%7Bname%7D 的組,它後續可以以這樣的方式被使用:
  • 在同一個模式字串中: equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%28%3FP%3Dname%29%7D%7D%7D 或者 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C1%7D%7D%7D
  • 通過匹配物件 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Bm%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Bm.group%28%27name%27%29%7D%7D%7D 或者 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Bm.end%28%27quote%27%29%7D%7D%7D
  • equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Bre.sub%7D%7D%7D 函式的換成的字串中: equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5Cg%3Cquoto%3E%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cg%3C1%3E%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5C1%7D%7D%7D

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%28%3F%3D...%29%7D%7D%7D :向右看匹配,但是本身不匹配任何字元,比如 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Bone%28%3F%3Dtwo%29%7D%7D%7D 會匹配所有後面跟子 equation?tex=%5Ctexttt%7Btwo%7Dequation?tex=%5Ctexttt%7Bone%7D

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%28%3F%21...%29%7D%7D%7D :否定的向右看匹配,但是本身不匹配任何字元,比如 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Bone%28%3F%21two%29%7D%7D%7D會匹配所有後面不跟著 equation?tex=%5Ctexttt%7Btwo%7Dequation?tex=%5Ctexttt%7Bone%7D

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%28%3F%3C%3D...%29%7D%7D%7D :向左看匹配,但是本身不匹配任何字元。

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%28%3F%3C%21...%29%7D%7D%7D :否定的向左看匹配,但是本身不匹配任何字元。

equation?tex=%5Cbbox%5B%23E8F8FF%2C+4px%2C+border%3A1px+solid+gray%5D%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%28%3F%28id%2Fname%29yes-pattern%7Cno-pattern%29%7D%7D%7D :如果 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Bid%7D%7D%7D 或者 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Bname%7D%7D%7D 代表的組存在那麼就表示 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Byes-pattern%7D%7D%7D ,否則就表示 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Bno-pattern%7D%7D%7D

字串:

字串有兩種,一種是

equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22...%22%7D%7D%7D 普通字串和 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Br%22...%22%7D%7D%7D 原始字串,它們的區別在於轉意義符號的處理上。

1. 普通字串:

首先需要說明的是

equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C%22%7D%7D%7D 是非法的,為了表示一個反斜槓,需要使用 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C%5C%5C%22%7D%7D%7D 來轉義,轉義的組合有:
a      b      f      n
N      r      t      u
U      v      x      
0      1      1      2
3      4      5      6
7      

equation?tex=%7B%5Ccolor%7Bred%7D%7B%E4%B8%8A%E9%9D%A2%E6%89%80%E6%9C%89%E6%83%85%E5%86%B5%E9%83%BD%E8%A1%A8%E7%A4%BA%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%EF%BC%81%7D%7D

而其餘的所有情況都被自動的理解為反斜槓加上字元,比如

equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5Cd%22%7D%7D%7D 被理解為 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5Cd%22%7D%7D%7D ,長度為兩個字元,並 equation?tex=%7B%5Ccolor%7Bred%7D%7B%5Csmall+%E4%B8%8D%E8%A1%A8%E7%A4%BA%7D%7D 字元轉義。

所以為了表示特殊字元中的數字

equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cd%7D%7D%7D ,我們可以使用 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5Cd%22%7D%7D%7D 或者 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C%5C%5Cd%22%7D%7D%7D ,這兩者是一模一樣的。
In : "d" == "d"
Out: True

比較難以理解的是數字表示的組的處理,為了表示第一組,我們需要使用

equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C%5C%5C1%22%7D%7D%7D ,這裡需要兩個反斜槓,因為一個反斜槓是轉義字元。但是為了表示第八組,我們就既可以使用 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C8%22%7D%7D%7D 也可以使用 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C%5C%5C8%22%7D%7D%7D ,這一點是比較奇怪的。
如果在表示特殊字元時,你不清楚是否被反斜槓轉義了,最安全的使用特殊字元的方式是使用兩個反斜槓。

注意到反斜槓在正則表示式中有兩個特殊作用:

  1. 是否表示轉義 - 如果不是就代表字面的反斜槓
  2. 是否表示特殊字元

因此為了匹配

equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C%22%7D%7D%7D 這個字串我們需要使用 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C%5C%5C%5C%5C%5C%5C%22%7D%7D%7D 來表示,比如:
In : re.fullmatch("", "")
Out: <re.Match object; span=(0, 1), match=''>

很容易引起困惑的是在第一次和第二次解析中都有意義的情況,比如

equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%5Cn%7D%7D%7D ,在正則表示式中 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5Cn%22%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C%5C%5Cn%22%7D%7D%7D 都表示換行,但是它們實實在在是兩個不同的字串:
In : "n" == "n"
Out: False
In : re.fullmatch("n", "n")
Out: <re.Match object; span=(0, 1), match='n'>
In : re.fullmatch("n", "n")
Out: No output

2. 原始字串

可以看到普通字串轉義符號需要兩次解析,這對會給書寫帶來很大的麻煩,這就是引入原始字串的初衷。原始字串中的一個反斜槓代表字面意義上的反斜槓,沒有轉義的意思。也就是說,原始字串等於把其中的一個轉義字元替換成兩個轉義字元的普通字元。比如下面的結果都為

equation?tex=%5Ctexttt%7BTrue%7D
r"1" == "1"
r"n" == "n"
r"" == ""

所以

equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5Cd%22%7D%7D%7Dequation?tex=+%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C%5C%5Cd%22%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Br%22%5Cd%22%7D%7D%7D 都是相同的, equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C8%22%7D%7D%7Dequation?tex=+%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C%5C%5C8%22%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Br%22%5C8%22%7D%7D%7D 都是相同的,但是 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C1%22%7D%7D%7Dequation?tex=+%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C%5C%5C1%22%7D%7D%7Dequation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Br%22%5C1%22%7D%7D%7D 中後兩個相同和第一個不同。

唯一的例外在於反斜槓跟著單引號或者雙引號時反斜槓會轉義後面的引號,但是反斜槓依然保留,比如

equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7Br%22%5C%22%22%7D%7D%7D 表示 equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%5C%22%7D%7D%7D 兩個字元,也就是說:
r""" == r'"' == """ == '"' == '"'

同時需要注意的是原始字串的每個反斜槓後面必須跟著一個字元,比如下面的寫法都是非法的:

r""
r""
r"1"

因此不存在一個原始字串等於普通字串

equation?tex=%7B%5Ccolor%7Bblue%7D%7B%5Ctexttt%7B%22%5C%5C%5C%5C%22%7D%7D%7D

當做正則表示式匹配時,推薦使用原始字串!