当前位置:网站首页>[regular expression] single character matching
[regular expression] single character matching
2022-07-27 15:47:00 【Grapefruit tree CC】
1. Match a single character
adopt re The module can complete the use of regular expressions to match strings
| Code | function |
|---|---|
| . | Match arbitrarily 1 Characters ( except \n) |
| [ ] | matching [ ] The characters listed in |
| \d | Match the Numbers , namely 0-9 |
| \D | Match non numeric , It's not numbers |
| \s | Match blanks , namely Space ,tab key |
| \S | Match non blank |
| \w | Match non special characters , namely a-z、A-Z、0-9、_、 Chinese characters |
| \W | Match special character , It's not a letter 、 The digital 、 Non Chinese characters |
Example 1: .
import re
ret = re.match(".","M")
print(ret.group())
ret = re.match("t.o","too")
print(ret.group())
ret = re.match("t.o","two")
print(ret.group())
Running results :
M
too
two
Example 2:[]
import re
# If hello The first character of is lowercase , So regular expressions need lowercase h
ret = re.match("h","hello Python")
print(ret.group())
# If hello The first character of is capitalized , So regular expressions need to be capitalized H
ret = re.match("H","Hello Python")
print(ret.group())
# Case write h All right
ret = re.match("[hH]","hello Python")
print(ret.group())
ret = re.match("[hH]","Hello Python")
print(ret.group())
ret = re.match("[hH]ello Python","Hello Python")
print(ret.group())
# matching 0 To 9 The first way to write it
ret = re.match("[0123456789]Hello Python","7Hello Python")
print(ret.group())
# matching 0 To 9 The second way
ret = re.match("[0-9]Hello Python","7Hello Python")
print(ret.group())
ret = re.match("[0-35-9]Hello Python","7Hello Python")
print(ret.group())
# The following rule can't match numbers 4, therefore ret by None
ret = re.match("[0-35-9]Hello Python","4Hello Python")
# print(ret.group())
Running results :
h
H
h
H
Hello Python
7Hello Python
7Hello Python
7Hello Python
Example 3:\d
import re
# A normal match
ret = re.match(" The goddess of the moon 1 Number "," The goddess of the moon 1 The launch was successful ")
print(ret.group())
ret = re.match(" The goddess of the moon 2 Number "," The goddess of the moon 2 The launch was successful ")
print(ret.group())
ret = re.match(" The goddess of the moon 3 Number "," The goddess of the moon 3 The launch was successful ")
print(ret.group())
# Use \d Match
ret = re.match(" The goddess of the moon \d Number "," The goddess of the moon 1 The launch was successful ")
print(ret.group())
ret = re.match(" The goddess of the moon \d Number "," The goddess of the moon 2 The launch was successful ")
print(ret.group())
ret = re.match(" The goddess of the moon \d Number "," The goddess of the moon 3 The launch was successful ")
print(ret.group())
Running results :
The goddess of the moon 1 Number
The goddess of the moon 2 Number
The goddess of the moon 3 Number
The goddess of the moon 1 Number
The goddess of the moon 2 Number
The goddess of the moon 3 Number
Example 4:\D
import re
match_obj = re.match("\D", "f")
if match_obj:
# Get matching results
print(match_obj.group())
else:
print(" Matching failure ")
Running results :
f
Example 5:\s
import re
# Spaces are white space characters
match_obj = re.match("hello\sworld", "hello world")
if match_obj:
result = match_obj.group()
print(result)
else:
print(" Matching failure ")
# \t It's a white space character
match_obj = re.match("hello\sworld", "hello\tworld")
if match_obj:
result = match_obj.group()
print(result)
else:
print(" Matching failure ")
Running results :
hello world
hello world
Example 6:\S
import re
match_obj = re.match("hello\Sworld", "hello&world")
if match_obj:
result = match_obj.group()
print(result)
else:
print(" Matching failure ")
match_obj = re.match("hello\Sworld", "hello$world")
if match_obj:
result = match_obj.group()
print(result)
else:
print(" Matching failure ")
Running results :
hello&world
hello$world
Example 7:\w
import re
# Match one of the non special characters
match_obj = re.match("\w", "A")
if match_obj:
# Get matching results
print(match_obj.group())
else:
print(" Matching failure ")
Execution results :
A
Example 8:\W
# Match one of the special characters
match_obj = re.match("\W", "&")
if match_obj:
# Get matching results
print(match_obj.group())
else:
print(" Matching failure ")
Execution results :
&
边栏推荐
猜你喜欢

【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版

学习Parquet文件格式

On juicefs

QT (XIII) qchart drawing line chart
![[TensorBoard] OSError: [Errno 22] Invalid argument处理](/img/bf/c995f487607e3b307a268779ec1e94.png)
[TensorBoard] OSError: [Errno 22] Invalid argument处理

What format is this data returned from the background

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

Is low code the future of development? On low code platform

Spark 3.0 DPP implementation logic

网络层的IP协议
随机推荐
【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
网络设备硬核技术内幕 路由器篇 22
Use deconstruction to exchange the values of two variables
First understanding of structure
Pictures to be delivered
《吐血整理》C#一些常用的帮助类
go语言慢速入门——基本内置类型
C语言:字符串函数与内存函数
Implement custom spark optimization rules
初识结构体
聊聊ThreadLocal
一文读懂鼠标滚轮事件(wheelEvent)
【剑指offer】面试题39:数组中出现次数超过一半的数字
后台返回来的是这种数据,是什么格式啊
Spark TroubleShooting整理
C:什么是函数中的返回值(转)
IP protocol of network layer
Extended log4j supports the automatic deletion of log files according to time division and expired files
Spark Bucket Table Join
Catalog component design and custom extended catalog implementation in spark3