python-Regex

來源:互聯網
上載者:User

標籤:多次   取數   第一個字元   正則表達   error   Regex   line   sre   int   

Regex,可以取資料

正則有匹配貪婪性,照多了匹配

 

>>> import re

>>> s="I am 19 years old"

>>> re.search(r"\d+",s)

<_sre.SRE_Match object at 0x0000000002F63ED0>

>>> re.search(r"\d+",s).group()

‘19‘

>>> 

>>> s="I am 19 years 20 old 30"

>>> re.findall(r"\d",s)

[‘1‘, ‘9‘, ‘2‘, ‘0‘, ‘3‘, ‘0‘]

>>> re.findall(r"\d+",s)

[‘19‘, ‘20‘, ‘30‘]

 

可以匹配資料,取資料,

判斷句子裡是否包含指定字串

re.match(r"\d+","123abc")裡面的r最好帶上,以防止逸出字元影響

r”\d”匹配數字

>>> re.match(r"\d+","123abc")

<_sre.SRE_Match object at 0x0000000002F63ED0>

>>> re.match("\d+","123abc")

<_sre.SRE_Match object at 0x000000000306B030>

>>> re.match("\d+","a123abc")

 

>>> re.match("\d+","a123abc")

>>> re.match("\d+","123abc").group()

‘123‘

>>> re.match("\d+","123abc 1sd").group()

‘123‘

Re.search(r“\d+”)任意位置符合就返回Re.findall(r“\d+”),也是任何位置匹配就返回Re.match(r“\d+”),從字串第一個位置開始匹配

>>> re.match("\d+","123abc 1sd").group()

‘123‘

>>> re.search("\d+","123abc 1sd").group()

‘123‘

>>> re.search("\d+","a123abc 1sd").group()

‘123‘

>>> re.match("\d+","a123abc 1sd").group()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: ‘NoneType‘ object has no attribute ‘group‘

>>> re.match("\d+","a123abc 1sd")

 

 

>>> re.findall(r"\d+","a1b2c3")

[‘1‘, ‘2‘, ‘3‘]

>>> re.match(r"\d+","a1b2c3")

r”\D”匹配非數字re.match(r"\D+","a1b2c3")匹配非數字

>>> re.match(r"\D+","a1b2c3").group()

‘a‘

>>> re.match(r"\D+","abc1b2c3").group()

‘abc‘

 

re.match(r"\D+\d+","ab12   bb").group()匹配”ab12  bb”,匹配ab12

>>> re.match(r"\D+\d+","ab12   bb").group()

‘ab12‘

 

r”\s”匹配空白re.match(r"\s+","s")匹配空白,match函數從字串的第一個字元就開始匹配

>>> re.match(r"\s+","ab12   bb").group()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: ‘NoneType‘ object has no attribute ‘group‘

>>> re.match(r"\s+","   bb").group()

 

r”\S”匹配非空白re.match(r"\S+","eee   bb")非空白,直到出現空白式

>>> re.match(r"\S+","eee   bb").group()

‘ee

 

>>> print ‘a‘#str方法

a

>>> ‘a‘#repr方法

‘a‘

r"\w+"匹配數字和字母re.findall(r"\w+","   sdf")數字和字母

>>> re.findall(r"\w+","   sdf")

[‘sdf‘]

>>> re.findall(r"\w+"," 12  sdf")

[‘12‘, ‘sdf‘]

>>> re.findall(r"\w+"," 12  sdf we")

[‘12‘, ‘sdf‘, ‘we‘]

r"\W+"匹配非數字和非字母re.match(r"\W+"," sf  fd").group()非數字和非字母

>>> re.match(r"\W+"," sf  fd").group()

‘ ‘

>>> re.match(r"\W+"," sf $% @# fd").group()

‘ ‘

>>> re.match(r"\W+","$#% sf $% @# fd").group()

‘$#% ‘

 

量詞

 

R”\w\w”取兩個

>>> re.match(r"\w\w","ww we").group()

‘ww‘

>>> re.match(r"\w\w","12 we").group()

‘12‘

r"\w{2}"取兩個

>>> re.match(r"\w{2}","12 we").group()

‘12‘

>>> re.match(r"\w{2}","123 we").group()

‘12‘

>>> re.match(r"\w{2}","1 23 we").group()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: ‘NoneType‘ object has no attribute ‘group‘

>>> re.match(r"\w{2,4}","1 23 we").group()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: ‘NoneType‘ object has no attribute ‘group‘

r"\w{2,4}",取2到4個,按最多的匹配

>>> re.match(r"\w{2,4}","123 we").group()

‘123‘

>>> re.match(r"\w{2,4}","12 we").group()

‘12‘

>>> re.match(r"\w{2,4}","123 we").group()

‘123‘

>>> re.match(r"\w{2,4}","1235 we").group()

‘1235‘

>>> re.match(r"\w{2,4}","12435 we").group()

‘1243‘

>>> re.match(r"\w{5}","12435 we").group()

‘12435‘

>>> re.match(r"\w{5}","srsdf we").group()

‘srsdf‘

>>> 

r"\w{2,4}?”抑制貪婪性,按最少的匹配

>>> re.match(r"\w{2,4}?","12435 we").group()

‘12‘

 

r"\w?"匹配0次和一次,如果沒有匹配的,返回空

0次也是匹配了,

>>> re.match(r"\w?","12435 we").group()

‘1‘

>>> re.match(r"\w?"," 12435 we").group()

‘‘

 

re.findall(r”\w?”,”12 we”)匹配到最後,沒有東西,0次,返回空

>>> re.findall(r"\w?"," 12 we")

[‘‘, ‘1‘, ‘2‘, ‘‘, ‘w‘, ‘e‘, ‘‘]

 

re.findall(r"\w"," 12 we")匹配一個

>>> re.findall(r"\w"," 12 we")匹配一個

[‘1‘, ‘2‘, ‘w‘, ‘e‘]

 

r"\w*"匹配0次或多次

>>> re.match(r"\w*"," 12435 we").group()

‘‘

>>> re.match(r"\w*","12435 we").group()

‘12435‘

>>> re.match(r"\w*"," 12435 we")

<_sre.SRE_Match object at 0x0000000002F63ED0>

>>> re.match(r"\w*","12435 we")

<_sre.SRE_Match object at 0x000000000306C030>

>>> re.match(r"\w*","12435 we").group()

‘12435‘

r"a.b"匹配ab間除斷行符號以外的任一字元

如果想匹配”a.b”,用r"a\.b"

>>> re.match(r"a.b","axb")
<_sre.SRE_Match object at 0x0000000001DCC510>
>>> re.match(r"a.b","a\nb")
>>> re.match(r"a\.b","a.b")
<_sre.SRE_Match object at 0x00000000022BE4A8>
>>> re.match(r"a\.b","axb")

 

>>> re.match(r".","axb").group()
‘a‘
>>> re.match(r"a.b","axb").group()
‘axb‘

 

練習匹配這個ip地址,規則就是有3個.和4段數,數字從0到255之間,即可

 

 

s="i find a ip:1.2.22.123 ! yes"

 

>>>re.search(r"\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}",s).group()

‘1.2.22.123‘

re.search(r"(\d{1,3}\.){3}\d{1,3}",s)分組匹配

>>>re.search(r"(\d{1,3}\.){3}\d{1,3}",s).group()

‘1.2.22.123‘

 

>>> re.search(r"(\d{1,3}\.){3}\d{1,3}",s).group()
‘1.2.22.123 

 

匹配多個字元的相關格式
字元 功能
* 匹配前?個字元出現0次或者?限次, 即可有可?
+ 匹配前?個字元出現1次或者?限次, 即?少有1次
? 匹配前?個字元出現1次或者0次, 即要麼有1次, 要麼沒有
{m} 匹配前?個字元出現m次
{m,} 匹配前?個字元?少出現m次
{m,n} 匹配前?個字元出現從m到n次

字元 功能
. 匹配任意1個字元(除了\n)
[ ] 匹配[ ]中列舉的字元
\d 匹配數字, 即0-9
\D 匹配?數字, 即不是數字
\s 匹配空?, 即 空格, tab鍵
\S 匹配?空?
\w 匹配單詞字元, 即a-z、 A-Z、 0-9、 _
\W 匹配?單詞字元

 

python-Regex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.