当前位置:网站首页>[regular expression] match the beginning and end
[regular expression] match the beginning and end
2022-07-27 15:47:00 【Grapefruit tree CC】
1. Match the beginning and the end
| Code | function |
|---|---|
| ^ | Match the beginning of a string |
| $ | Match string end |
Example 1:^
demand : Match data that starts with a number
import re
# Match data that starts with a number
match_obj = re.match("^\d.*", "3hello")
if match_obj:
# Get matching results
print(match_obj.group())
else:
print(" Matching failure ")
Running results :
3hello
Example 2:$
demand : Match data ending in numbers
import re
# Match data ending in numbers
match_obj = re.match(".*\d$", "hello5")
if match_obj:
# Get matching results
print(match_obj.group())
else:
print(" Matching failure ")
Running results :
hello5
Example 3:^ and $
demand : Match the middle content starting with a number, regardless of ending with a number
match_obj = re.match("^\d.*\d$", "4hello4")
if match_obj:
# Get matching results
print(match_obj.group())
else:
print(" Matching failure ")
Running results :
4hello4
2. Matches all but the specified characters
[^ Specify the characters ]: Indicates that all characters except the specified ones match
demand : The first character except aeiou All characters match
import re
match_obj = re.match("[^aeiou]", "h")
if match_obj:
# Get matching results
print(match_obj.group())
else:
print(" Matching failure ")
Execution results
h
边栏推荐
猜你喜欢
随机推荐
实体类(VO,DO,DTO)的划分
一文读懂鼠标滚轮事件(wheelEvent)
【剑指offer】面试题49:丑数
It is said that the US government will issue sales licenses to Huawei to some US enterprises!
【剑指offer】面试题54:二叉搜索树的第k大节点
【云享读书会第13期】音频文件的封装格式和编码格式
Using Prometheus to monitor spark tasks
Troubleshooting the slow startup of spark local programs
传美国政府将向部分美企发放对华为销售许可证!
后台返回来的是这种数据,是什么格式啊
【剑指offer】面试题51:数组中的逆序对——归并排序
【云享读书会第13期】视频文件的编码格式
【剑指offer】面试题39:数组中出现次数超过一半的数字
[Yunxiang book club issue 13] common methods of viewing media information and processing audio and video files in ffmpeg
网络原理(2)——网络开发
Spark Bucket Table Join
JS find the maximum and minimum values in the array (math.max() method)
HJ8 合并表记录
js使用一元运算符简化字符串转数字
The shell script reads the redis command in the text and inserts redis in batches









