Boundary representation of regular expressions

Next, learn about the boundary representation of regular expressions. The boundary representation is to limit the start and end boundaries of the matched string when writing regular expressions.

Character description
^ matches the beginning of the string
$ matches the end of the string
\b matches a word boundary
\B matches a non-word boundary
Note: boundary characters are only used to describe boundary information and cannot be used for character matching.

Example: Improve the example of mobile phone number matching in the previous lesson. If the mobile phone number with more than 11 digits is matched, or there are other characters after the mobile phone number with 11 digits, the regular expression can also match successfully. of. In this case, the end of the mobile phone number needs to be restricted.

#使用$限制匹配的字符串以11位数字组成,结尾不能添加其他字符
rs = re.match("1[3578]\d{9}$","13612345678") 
print(rs.group()) #匹配正确的手机号
#手机号末尾添加字符串将匹配失败
rs = re.match("1[3578]\d{9}$","13612345678abc")#匹配失败
print(type(rs)) #空类型

operation result:

13612345678
<class'NoneType'>
(Use the match method for regular matching. "^" The beginning of the match is not very obvious, because the match itself is matched in order from left to right)

Example: Mailbox matching

#邮箱地址以3到10个单词字符开始,以@163.com结束
rs = re.match("\w{3,10}@163.com$","[email protected]")
print(rs)
rs = re.match("\w{3,10}@163.com$","[email protected]")
print(rs)
rs = re.match("\w{3,10}@163.com$","[email protected]")
print(rs)
rs = re.match("\w{3,10}@163.com$","[email protected]")
print(rs)

operation result:

<_sre.SRE_Match object; span=(0, 17), match='[email protected]'>
None
None
None
Question 1: Replace the "." in the mailbox with a letter at random will also match successfully

rs = re.match("\w{3,10}@163.com$","hello_123@163hcom")
print("匹配结果:%s"%rs)

operation result:

Matching result: <_sre.SRE_Match object; span=(0, 17), match='hello_123@163hcom'> The
reason is: "." in the regular expression has a special meaning, which means any character except \n

Solution: Use the escape character \ to identify the "." is a normal character "."

rs = re.match("\w{3,10}@163\.com$","[email protected]")
print("匹配结果1:%s"%rs)
rs = re.match("\w{3,10}@163\.com$","hello_123@163hcom")
print("匹配结果2:%s"%rs)

operation result:

Matching result 1: <_sre.SRE_Match object; span=(0, 17), match='[email protected]'>
Matching result 2: None

Guess you like

Origin blog.csdn.net/lildn/article/details/114528751