Re: [問題] python Regular Expression 用法

看板Python作者 (可愛小孩子)時間7年前 (2017/06/09 17:21), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串2/2 (看更多)
詳細意義:https://docs.python.org/2.0/ref/strings.html 不過今天要 match 的 pattern: 'www\.(.+)\.com' 前面有沒有加上 r 答案都是一樣的 ------------------------------- 來個簡單的測試(可以較明顯看出差異): import re ############### # 4 backslashes word = r'\\\\' ############################## # 以下為 prefix 'r' 有無的差異 pattern1 = '\\\\' pattern2 = r'\\\\' print('word: ' + word) print('pattern1: ' + pattern1) print('pattern2: ' + pattern2) m = re.match(pattern1, word) print('pattern1 matching: ' +m.group(0)) m = re.match(pattern2, word) print('pattern2 matching: ' +m.group(0)) ----- 結果: word: \\\\ pattern1: \\ pattern2: \\\\ pattern1 matching: \ pattern2 matching: \\ ※ 引述《asiagodfater (亞洲統粉前來報到)》之銘言: : 請問一下各位大大 : 小弟在網路上有找到很多的範例 : 但是大家在re.match()裡面會加一個r : 這個r是什麼意思呢?! : 很多範例都有但是好像沒有解釋r是什麼 : 下面就是範例之一,在re.match裡面的最前面也有一個r那個r是代表什麼意思呢!? : m = re.match(r'www\.(.+)\.com', 'www.google.com') : m.group(0) : # result : 'www.google.com' : m.group(1) : # result : 'google' -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.61.233.210 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1497000066.A.6E9.html

06/09 18:31, , 1F
raw string跟re沒有關係
06/09 18:31, 1F

06/09 23:03, , 2F
推樓上(Y)
06/09 23:03, 2F
文章代碼(AID): #1PEcY2Rf (Python)
文章代碼(AID): #1PEcY2Rf (Python)