場景一:比如有個需求,判斷使用者輸入的密碼是否是6-15位的?
場景二:需要將new_str=『today126782is83272monday』,轉化成today is monday,如何快速實現?
場景三:自動化測試中需要將用例中特定的內容進行引數化,如何實現?
場景四:web自動化要匹配前端**中是否有某個內容,如何實現?
…正規表示式是:
一串描述文字規則的**可進行複雜的字串搜尋和替換任何程式語言都支援絕大多數文字編輯器都支援常用匹配單個字元表示式:
字元功能
.匹配任意1個字元(除了\n)
[ ]匹配[ ]中列舉的字元
\d匹配數字,即0-9
\d匹配非數字,即不是數字
\s匹配空白,即空格,tab鍵
\s匹配非空白
\w匹配單詞字元,即a-z、a-z、0-9、_
\w匹配非單詞字元
操作演練:
1a. 匹配任意字元:.
one_str = "today is monday!"
result = re.match("t.day", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
執行結果:today
1b. 匹配任意字元時\n不能匹配到
one_str = "t\noday is monday!"
result = re.match("t.day", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
執行結果:未匹配上!
2. 匹配中列舉的字元
one_str = "today is monday!"
# 使用match方法來匹配
# 如果能正常匹配,則返回match,並且group()
# 如果不能匹配,則返回的none
result = re.match("t[abo]day", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
執行結果:today
3. 匹配數字,即0-9
one_str = "today2 is monday!"
# 使用match方法來匹配
# 如果能正常匹配,則返回match,並且group()
# 如果不能匹配,則返回的none
result = re.match(r"today\d", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
執行結果:today2
4. 匹配單詞字元
one_str = "today2 is monday!"
# 使用match方法來匹配
# 如果能正常匹配,則返回match,並且group()
# 如果不能匹配,則返回的none
result = re.match(r"t\wday\d", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
執行結果:today2
常用匹配單個字元表示式:
字元功能
*匹配前乙個字元出現0次或者無限次,即可有可無
+匹配前乙個字元出現1次或者無限次,即至少有1次
?匹配前乙個字元出現1次或者0次,即要麼有1次要麼沒有
匹配前乙個字元出現m次
匹配前乙個字元出現從m到n次
操作演練
1. 至少匹配一次:+
one_str = "today2 is monday!"
# 使用match方法來匹配
# 如果能正常匹配,則返回match,並且group()
# 如果不能匹配,則返回的none
result = re.match(r"today2\w+", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
執行結果:未匹配上!
2. 要麼匹配一次要麼匹配0次:?
one_str = "today2 is monday!"
# 使用match方法來匹配
# 如果能正常匹配,則返回match,並且group()
# 如果不能匹配,則返回的none
result = re.match(r"today2\w?", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
執行結果:today2
3. 匹配次
one_str = "today2 is monday!"
# 使用match方法來匹配
# 如果能正常匹配,則返回match,並且group()
# 如果不能匹配,則返回的none
result = re.match(r"t\w", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
匹配5次後,執行結果:today2
4. 匹配次
one_str = "today2 is monday!"
# 使用match方法來匹配
# 如果能正常匹配,則返回match,並且group()
# 如果不能匹配,則返回的none
result = re.match(r"t\w", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
如上**,匹配,執行結果:toda
1. search方法
查詢特定字串是否存在(只要找到字元就返回)
one_str = "today2 is monday!"
result = re.search(r'mon\w+', one_str)
if result:
print(result.group())
else:
print("未匹配上!")
執行結果:monday
2. sub方法
將匹配到的資料進行替換
one_str = "today is monday!"
result = re.sub(r'mon\w+', 'tuesday', one_str)
if result:
print(result)
else:
print("未匹配上!")
執行結果:today is tuesday!
3. split方法
根據匹配到的特定分隔符進行切割字串,並返回乙個列表
例如:將下列字串分割後顯示:today is monday
target_str = 'today126782is83272monday'
new_list = re.split(r'\d+', target_str) # 使用正則匹配後進行分割
new_str = (' '.join(new_list))
print(new_str)
執行結果:today is monday 正規表示式的簡單介紹
一般用於轉義字元 斷言目標的開始位置 或在多行模式下是行首 斷言目標的結束位置 或在多行模式下是行尾 匹配除換行符外的任何字元 預設 開始字元類定義 結束字元類定義 開始乙個可選分支 子組的開始標記 子組的結束標記 作為量詞,表示 0 次或 1 次匹配。位於量詞後面用於改變量詞的貪婪特性。查閱量詞 ...
Java正規表示式簡單介紹
所謂的正規表示式就是用來操作字串的,正規表示式雖然簡化了 功能的實現,但是閱讀性變得較差,所以要能熟練的使用的正則表達的就必須要熟練的記住具體符號的含義。eg1 public class regexdemo 正規表示式。boolean b qq.matches regex system.out.pr...
正規表示式 簡單使用
一 校驗數字的表示式 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 有兩位小數的...