当前位置:网站首页>[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
边栏推荐
猜你喜欢
随机推荐
JS uses unary operators to simplify string to number conversion
【剑指offer】面试题41:数据流中的中位数——大、小堆实现
Spark 3.0 DPP implementation logic
表格插入行内公式后,单元格失去焦点
网络原理(1)——基础原理概述
【云享读书会第13期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
Spark 3.0 testing and use
【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针
js使用for in和for of来简化普通for循环
【剑指offer】面试题53-Ⅱ:0~n-1中缺失的数字——二分查找
[Yunxiang book club issue 13] coding format of video files
C语言:三子棋游戏
[正则表达式] 匹配多个字符
C语言:字符串函数与内存函数
Is low code the future of development? On low code platform
On juicefs
折半插入排序
【剑指offer】面试题46:把数字翻译成字符串——动态规划
JS uses extension operators (...) to simplify code and simplify array merging








