re正規表示式

2022-08-12 01:48:15 字數 3514 閱讀 8869

import re

s = "hello_宇霖_hello"

print(re.findall("hello", s))

import re

s = "hello_宇霖_hello"

print(re.findall("\w", s))

import re

s = "hello_宇霖_hello123!@#$"

print(re.findall("\w", s))

import re

s = "hello 宇霖 hello\n\t"

print(re.findall("\s", s))

import re

s = "hello 宇霖 hello\n\t"

print(re.findall("\s", s))

import re

s = "hello 宇霖 hello123\n\t"

print(re.findall("\d", s))

import re

s = "hello 宇霖 hello123\n\t"

print(re.findall("\d", s))

import re

s = "hello宇霖_123hello\t\n"

print(re.findall("\ahello", s))

print(re.findall("^hello", s)) # 常用

import re

s = "hello宇霖_123hello"

print(re.findall("o\z", s))

print(re.findall("o$", s)) # 常用

import re

s = "hello宇霖_123hello\t\n"

print(re.findall("\n", s))

print(re.findall("\t", s))

import re

s = "h\nllo宇霖_123hallo\t\n"

print(re.findall("h.l", s))

# re.dotall : 修改非換行符bug

s = "h\nllo宇霖_123hello\t\n"

print(re.findall("h.l", s, re.dotall))

import re

s = "hello宇霖_1a-2b-3c hello"

print(re.findall("[a-z]", s)) # 小寫的a-z

print(re.findall("[a-z]", s)) # 大寫的a—z

print(re.findall("[a-za-z]", s)) # 大小寫的a-z a-z

print(re.findall("[a-za-z0-9]", s)) # 大小寫的a-z a-z 0-9

import re

s = "hello宇霖_1a-2b-3c hello"

print(re.findall("[^0-9]",s))

import re

s = "hello宇霖_1a-2b-3c hellohhhohh"

print(re.findall("h*", s))

import re

s = "hello宇霖_1a-2b-3c hellohhhohh"

print(re.findall("h+", s))

import re

s = "hello宇霖_1a-2b-3c hellohhhohh"

print(re.findall("h?", s))

import re

s = "hello宇霖_1a-2b-3c hellohhhohh"

print(re.findall("h", s))

import re

s = "hello宇霖_1a-2b-3c hellohhhohh"

print(re.findall("h", s)) # h h hh hh

import re

s = "hello宇霖_1a-2b-3c hellohhhohh"

print(re.findall("h|o", s))

import re

s = "hello宇霖_1a-2b-3c hello hhhohh"

print(re.findall("h(...)o", s))

import re

s = "hello yulin hello"

print(re.search("hello", s).group())

import re

s = "hello yulin hello"

print(re.match("hello", s).group())

import re

s = "hello yulin,hello!world;50"

print(re.split("[#,!; ]", s))

import re

print(re.sub("10", "yulin", "10是個靚仔。"))

import re

obj = re.compile("\w")

print(obj.findall("hello_yulin_hello"))

import re

g = re.finditer("\w", "hello_yulin_hello")

for i in g:

print(i.group())

import re

s = "1-2*(60+(-40.35/5)-(-4*3)"

# 找整數

print(re.findall("\d+", s))

# 找所有的數字(包含小數)

print(re.findall("\d+\.\d+|\d+", s))

# 找所有的數字(包含小數和負數)

print(re.findall("-?\d+\.\d+|-?\d+", s))

import re

qq = input("請輸入qq號:")

print(re.findall("[1-9][0-9]", qq))

正規表示式 RE

最近一段時間在研究nginx的rewirte重寫機制,因此對re需要有一定的了解,看了想關的文章,因此自己來寫一篇類似總結性的的文章。基本來說,正規表示式是一種用來描述一定數量文字的模式。regex regular express。本文用 regex 來表示一段具體的正規表示式。一段文字就是最基本的...

re正規表示式

1.數字 0 9 2.n位的數字 d 3.至少n位的數字 d 4.m n位的數字 d 5.零和非零開頭的數字 0 1 9 0 9 6.非零開頭的最多帶兩位小數的數字 1 9 0 9 0 9 7.帶1 2位小數的正數或負數 d d 8.正數 負數 和小數 d d 9.有兩位小數的正實數 0 9 0 9...

Re正規表示式

import re 匯入re模組 重複出現的字串 對於重複出現的字串可以用大括號內部加上重複次數的方式表達 r d 分組 使用小括號分組 r d d 重複出現的字串 對於重複出現的字串可以用大括號內部加上重複次數的方式表達 r d 重複出現的字串 對於重複出現的字串可以用大括號內部加上重複次數的方式...