当前位置:网站首页>[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
边栏推荐
猜你喜欢

QT (IV) mixed development using code and UI files

复杂度分析

网络原理(1)——基础原理概述

【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN

C language: function stack frame

/Dev/loop1 takes up 100% of the problem

学习Parquet文件格式

面试重点——传输层的TCP协议

HaoChen CAD building 2022 software installation package download and installation tutorial

【剑指offer】面试题45:把数组排成最小的数
随机推荐
Jump to the specified position when video continues playing
线程间等待与唤醒机制、单例模式、阻塞队列、定时器
直接插入排序
Catalog component design and custom extended catalog implementation in spark3
multimap案例
$router.back(-1)
Extended log4j supports the automatic deletion of log files according to time division and expired files
Read the wheelevent in one article
Implementation of spark lazy list files
[正则表达式] 匹配分组
学习Parquet文件格式
C language: factorial recursive implementation of numbers
Summer Challenge harmonyos realizes a hand-painted board
synchronized和ReentrantLock的区别
On juicefs
Go language learning notes (1)
多线程带来的的风险——线程安全
The difference between synchronized and reentrantlock
【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针
语音直播系统——提升云存储安全性的必要手段