當使用re.findall(pattern ,s)時候
如果在表示式中包含有() ,則只會輸出括號裡面的內容,如何既使用() 又不會只返回括號裡面的內容呢?
大俠,在這裡隆重推薦 (?: )這種方法來代替 ()。
下面看案例:
以上**,執行結果是:import re
"""tips:
#1 re.findall(pattern,string) 選取所有合適的條目,如果用了括號,則只顯示括號內部的
如果不想只顯示部分 可用(?:pattern)來代替(pattern)
"""s=
" industries industry industry industries industriea industrie4 "
pattern1=re.
compile
('industr(?:y|ies)'
,flags=re.m)
pattern2=re.
compile
('industr(y|ies)'
,flags=re.m)
res1=re.findall(pattern1,s)
res2=re.findall(pattern2,s)
(res1)
(res2)
---------------------------------------------大俠專用分隔符-----------------------------------------|0|[
'industries'
,'industry'
,'industry'
,'industries'][
'ies'
,'y'
,'y'
,'ies'
][finished in
0.0s]
到此,各位少俠的(?:pattern)就介紹到這裡。
**江湖漫漫,少俠下期再見!
正規表示式之Pattern與Matcher
pattern類 描述匹配模式。matcher類 具體的匹配器,依賴於pattern類提供的匹配模式。pattern類的建立 由於pattern的構造器私有,所以一般通過pattern類的靜態方法compile string regex 來獲得物件,該方法需要傳入乙個正規表示式的字串作為匹配規則。也...
正規表示式 符號 解釋
可以到這裡學習 字元描述 將下乙個字元標記為乙個特殊字元 或乙個原義字元 或乙個 向後引用 或乙個八進位制轉義符。例如,n 匹配字元 n n 匹配乙個換行符。序列 匹配 而 則匹配 匹配輸入字串的開始位置。如果設定了 regexp 物件的 multiline 屬性,也匹配 n 或 r 之後的位置。匹...
正規表示式字元解釋
字元 描述 將下乙個字元標記為乙個特殊字元 或乙個原義字元 或乙個 向後引用 或乙個八進位制轉義符。例如,n 匹配字元 n n 匹配乙個換行符。序列 匹配 而 則匹配 匹配輸入字串的開始位置。如果設定了 regexp 物件的 multiline 屬性,也匹配 n 或 r 之後的位置。匹配輸入字串的結...