2 python string common operations: regular expressions, using%

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_41611045/article/details/95722512

A regular expression
regular expression is a string that matches the pattern python, including specific string search, replace, etc. operations, wherein the presence of regular expressions python re module, there are the following four functions
1, search (pattern, string) in the string to find a match
2, findall (pattern, string, flags = 0) match is found, return a list of all matching section
3, sub (pattern, repl, string, count = 0, flags = 0 ) in the string matches the regular expression is partially replaced other values
4, split (pattern, string, maxsplit = 0, flags = 0) is divided according to the matching string, returns a list of strings of the separator

1, Search
Search string search similar to the find, the string find there str.find (string), returns the index of the string if the string is found where, otherwise it returns -1, for example:,

import re
str1='imooc video=1000' 
str1.find("1000")
#结果12
str1.find("lalal")
#结果-1

②search, search in the search string, after completion of printing Soso group ()
Example 1: In str1 = 'imooc video = 1000' 1000 searches

a=re.search("1000",str1)
a.group()
#结果:'1000'

Note If you can not search for words, while not use .group () will print an error

Example 2: In search pengchuang "Peng break" in

str1="彭闯"
a=re.search("pengchuang",str1,flags=1)
print(a)
#结果:None

Example 3: In str1 = 'java = 1000, python = 9090' search for any number

str1='java=1000,python=9090'  
info=re.search(r'\d+',str1)#'\d'代表任意一个数字,'+'可以匹配数字一次或者无限次,只能匹配最前
info.group()
结果:1000'

③match (pattern, string)
the same python in there match to match, but the match is to start from the beginning of the comparison, and search from the beginning than the whole string, such as
re.match ( "1000", "lalal1000 ") return None

Summary:
①re.search (pattern, string), look for the string pattern in string, if a group () print string is found, if not found returns None, only matched once, and returned to find it, and find () difference, find does not support regular expressions
② python has the same match to match, but the match is to start from the beginning of the comparison, and search from the beginning than to the entire string

2, findall (pattern, string)
match is found, returns a list of all matching parts

str1="彭闯"
a=re.findall("pengchuang",str1,flags=1)
print(a)
#结果:[]

str1='java=1000,python=9090'  
info=re.findall(r'\d+',str1)#'\d'代表任意一个数字,'+'可以匹配数字一次或者无限次,只能匹配最前
print(info)
#结果:['1000', '9090']

str1="1 0 0 0 "
info=re.findall(r'\d+',str1)#'\d'代表任意一个数字,'+'可以匹配数字一次或者无限次,只能匹配最前
print(info)
#结果:['1','0','0','0']

findall will match all characters found, if the find will return a list of all the characters, if not found, it returns []

Summary: findall will match all characters, if the return will find a list of all the characters, if not found, it returns []

. 3, Sub (the pattern1, pattern2, String)
Sub (matching character, replacing the character, string, count = 0)
in the string matches the regular expression is partially replaced other values

1:替换字符串中pengchuang为彭闯
str1="pengchuang is a good boy"
info=re.sub('pengchuang',"彭闯",str1)#
print(info)
#结果:彭闯 is a good boy

② replace them count parameter represents the number of times, count = 1, replaced once, and so on, count = 0 Replace All

str1="pengchuang is a good boy,pengchuang is a student"
info=re.sub('pengchuang',"彭闯",str1,count=1)#
print(info)
#'彭闯 is a good boy,pengchuang is a student'

4, split () split character

(1) Split
① Normal Match
Example 1:
dividing space string

str1 = "Line1-abcdef \nLine2-abc \nLine4-abcd"
print(str1)
print(str1.split())

结果
Line1-abcdef Line2-abc Line4-abcd
[‘Line1-abcdef’, ‘Line2-abc’, ‘Line4-abcd’]

Example 2: dividing string ||

str7='我是||南京||邮电大学的学生'
print(str7)
#str7.split(r'||')
print(str7.split('||'))

[ 'I am', 'Nanjing', 'student at the University of Posts and Telecommunications']

Example 3: | divided string

str8='我是|南京|邮电大学的学生'
print(str8)
#str7.split(r'||')
print(str8.split('|'))

[ 'I am', 'Nanjing', 'student at the University of Posts and Telecommunications']

Note: split Split string by default all spaces
② on translation slash
Example 4: \ to cut strings

str9='我是\南京\邮电大学的学生'
print(str9)
#str7.split(r'||')
print(str9.split('\\'))

Results:
I \ Nanjing \ University of Posts and Telecommunications students
[ 'I am', 'Nanjing', 'student at the University of Posts and Telecommunications']
Example 5: double \ character segmentation

str10=r'我是\\南京\\邮电大学的学生'
print(str10)
print(str10.split(r"\\"))

Results:
I \ Nanjing \ University of Posts and Telecommunications students
[ 'I am', 'Nanjing', 'student at the University of Posts and Telecommunications']

(2) re.split
re.split (pattern, String, maxsplit is = 0, the flags = 0) is divided according to the matching string, returns a list of strings of the separator
maxsplit: Maximum number of division
Example. 1:
① plurality of divided delimiter

str6='imooc:java c++ python c#'  
info=re.split(r':| ',str6)#
#利用:和空格分隔
print(info)
#结果:['imooc', 'java', 'c++', 'python', 'c#']

② translation on special symbols
Example 2: || regular expression

str1=r'我是||南京||邮电大学的学生'
print(str1)
info=re.split("\|\|",str1)#
print(info)

| In the regular expression there is a special meaning, so there is a direct regular expression with || representatives, coming | into symbols
Example 3: \ regular expression

str2=r'我是\南京\邮电大学的学生'
print(str2)
info=re.split("\\\\",str2)#
print(info)

方法2:
str2=r'我是\南京\邮电大学的学生'
print(str2)
info=re.split(r"\\",str2)#
print(info)

The first one is the first in a string CKS expression, i.e. \\ = \, then the regular expression of regular expression \, \ Representative
Example 4: \ segmentation

str3=r'我是\\南京\\邮电大学的学生'
print(str3)
info=re.split(r"\\\\",str3)#
print(info)
#结果:['我是', '南京', '邮电大学的学生']

Example 5: / segmentation and dividing //

str4=r'我是/南京/邮电大学的学生'
print(str4)
info=re.split("/",str4)#
print(info)

str5=r'我是//南京//邮电大学的学生'
info=re.split(r"//",str5)#
print(info)

#结果:['我是', '南京', '邮电大学的学生']
#	 ['我是', '南京', '邮电大学的学生']

It can be seen as long as not a regular expression special characters, without direct translation.
(. 3) split () and the difference between re.split
split () is set to a fixed delimiter for segmentation, it does not support regular expression, regular expression and re.split splitting.

Summary:
① before the string plus r represent each character in the string is a separate free regular expression
such as: print (r "Hello \ n") Result: Hello \ n, that is, \ n is the string
②python in \ represents the translation of the meaning, on behalf of the special character back into a string, to pay particular attention here, there will be a regular expression \ this special symbols
Second, regular expressions
1, the regular expression refers to the use of specific to match format string

字符    功能
.       匹配任意1个字符(除了\n)
[]      匹配[ ]中列举的单个字符
\d      匹配数字,也就是0-9
\D      匹配非数字,也就是匹配不是数字的字符
\s      匹配空白符,也就是空格\tab
\S      匹配非空白符,\s取反
\w      陪陪单词字符, a-z, A-Z, 0-9, _
\W      匹配非单词字符, \w取反

2、

字符    功能
*       匹配前一个字符出现0次多次或者无限次,可有可无,可多可少
+       匹配前一个字符出现1次多次或则无限次,直到出现一次
?       匹配前一个字符出现1次或者0次,要么有1次,要么没有
{m}     匹配前一个字符出现m次
{m,}    匹配前一个字符至少出现m次
{m,n}   匹配前一个字符出现m到n次

Example 1: \ string dividing

Third, the use of%
1, modulo operation
in python fetch operations for the% remaining.
such as:

5%3
输出:2

Representative 5 divided by other 3 2
2 string operator
syntax string operators% is:

%[flags] [width].[precision] typecode

(name)为命名
flags可以有+-' '0+表示右对齐。-表示左对齐。' '为一个空格,表示在正数的左侧填充一个空格,从而与负数对齐。0表示使用0填充。
width表示显示宽度
precision表示小数点后精度
后面再加%使用字符串

以下是类型码
%s    字符串 (采用str()的显示)
%r    字符串 (采用repr()的显示)
%c    单个字符
%b    二进制整数
%d    十进制整数
%i    十进制整数
%o    八进制整数
%x    十六进制整数
%e    以e的科学计数法表示
%E    以E的科学计数法表示
%f    浮点数
%F    浮点数,与上相同%g    指数(e)或浮点数 (根据显示长度)
%G    指数(E)或浮点数 (根据显示长度)
%%    字符"%"

(1)% s,% r Usage

name='彭闯'
print('我叫%s'%name)
print('我叫%r'%name)

My name is Peng break
my name 'Pang Chuang'
(2)% c usage

name='彭'
print('我叫%c'%name)

My name is Peng
number (3) converts the input into the development of a string of digits

#进制表示方式
二进制: 0b             
八进制: 0
十六进制: 0x
#age为16进制数字
age=0x20
#八进制
print('我今年%o岁'%age)
我今年40#十进制
print('我今年%d岁'%age)
我今年32#十进制
print('我今年%i岁'%age)
我今年32#十六进制
print('我今年%x岁'%age)
我今年20

(4) $ e,% E Usage

num=2
print('%e'%num)
print('%E'%num)

+ 00 2.000000e
2.000000E + 00
(5) of floating-point representation

price=16
print("这碗面%.2f"%price)

This bowl 16.00

price=16
#占用7个宽度,2位精确度
print("这碗面%7.2f"%price)
这碗面  16.00
```py
#用空格填充
price=16
print("这碗面%  7.2f"%price)
#左对齐
price=16
print("这碗面%+7.2f"%price)
#右对齐
price=16
print("这碗面%-7.2f"%price)
#用0填充
price=16
print("这碗面%07.2f"%price)

This bowl 16.00
this bowl +16.00
this bowl 16.00
this bowl 0016.00
(6) a plurality of replacement values

"%s %d %f"%("abc",123,3.21)#多个值
'abc 123 3.210000'

(7) translation%, if the string contains% how to do. Then you can take the percent translates way to carry out
such

<1>
num=17
print("今年公司的利润上升了%s%%"%num)
<2>
num=17
print("今年公司的利润上升了%s%%%%"%num)

This year the company's profit rose 17%
this year, the company's profit rose 17 %%
Analysis:% has always been a special time to be translated characters written translation in%, if there are two percent of the translation with the two characters%.

Guess you like

Origin blog.csdn.net/weixin_41611045/article/details/95722512