模块的的作用主要是用于字符串和文本处理,查找,搜索,替换等
复习一下基本的正则表达式吧
.:匹配除了换行符以为的任意单个字符
*:匹配任意字符,一个,零个,多个都能匹配得到 俗称贪婪模式
+:匹配位于+之前的一个或者多个字符
|:匹配位于|之前或者之后的字符
^:匹配行首
$:匹配行尾
?:匹配位于?之前的零个或者一个字符,不匹配多个字符
\:表示 \ 之后的为转义字符
[]:匹配[]之中的任意单个字符,[0-9]表示匹配0到9任意一个数字
():将位于()之内的的内容当作一个整体
{}:按{}中的次数进行匹配,100[0-9]{3}表示在100之后任意匹配一个3位数(100-999)
python中以\开头的元字符:
特殊序列符号 |
意义 |
\A |
只在字符串开始进行匹配 |
\Z |
只在字符串结尾进行匹配 |
\b |
匹配位于开始或结尾的空字符串 |
\B |
匹配不位于开始或结尾的空字符串 |
\d |
相当于[0-9] |
\D |
相当于[^0-9] |
\s |
匹配任意空白字符:[\t\n\r\r\v] |
\S |
匹配任意非空白字符:[^\t\n\r\r\v] |
\w |
匹配任意数字和字母:[a-zA-Z0-9] |
\W |
匹配任意非数字和字母:[^a-zA-Z0-9] |
正则表达式语法表
语法 | 意义 | 说明 |
"." | 任意字符 |
|
"^" | 字符串开始 | '^hello'匹配'helloworld'而不匹配'aaaahellobbb' |
"$" | 字符串结尾 | 与上同理 |
"*" |
0 个或多个字符(贪婪匹配) |
匹配 |
"+" |
1 个或多个字符(贪婪匹配) |
与上同理 |
"?" |
0 个或多个字符(贪婪匹配) |
与上同理 |
*?,+?,?? |
以上三个取第一个匹配结果(非贪婪匹配) |
匹配 |
{m,n} |
对于前一个字符重复m到n次,{m}亦可 |
a{6}匹配6个a、a{2,4}匹配2到4个a |
{m,n}? |
对于前一个字符重复m到n次,并取尽可能少 |
‘aaaaaa'中a{2,4}只会匹配2个 |
"\\" |
特殊字符转义或者特殊序列 |
|
[] |
表示一个字符集 | [0-9]、[a-z]、[A-Z]、[^0] |
"|" |
或 | A|B,或运算 |
(...) |
匹配括号中任意表达式 |
|
(?#...) |
注释,可忽略 |
|
(?=...) |
Matches if ... matches next, but doesn't consume the string. |
'(?=test)' 在hellotest中匹配hello |
(?!...) |
Matches if ... doesn't match next. |
'(?!=test)' 若hello后面不为test,匹配hello |
(? |
Matches if preceded by ... (must be fixed length). |
'(?)test' 在hellotest中匹配test |
(? |
Matches if not preceded by ... (must be fixed length). |
'(?test' 在hellotest中不匹配test |
匹配的标志和含义
标志 | 含义 |
re.I | 忽略大小写 |
re.L | 根据本地设置而更改\w,\W,\b,\B,\s,\S的匹配内容 |
re.M | 多行匹配模式 |
re.S | 使“.”元字符匹配换行符 |
re.U | 匹配Unicode字符 |
re.X | 忽略需要匹配模式中的空格,并且可以使用"#"号注释 |
文本内容(提取Linux下的password文件)
man:x:6:12:man:/var/cache/man:/bin/nologin
re模块中有3个搜索函数,每个函数都接受3个参数(匹配模式,要匹配的字符串,进行匹配的标志),如果匹配到了就返回一个对象实例,么有就返会None.
findall():用于在字符串中查找符合正则表达式的字符串,并返回这些字符串的列表
search():搜索整个字符串,返回对象实例
match():只从第一个字符开始匹配,后面的不再匹配,返回对象实例
lovelinux@LoveLinux:~/py/boke$ cat text man:x:6:12:man:/var/cache/man:/bin/sh lovelinux@LoveLinux:~/py/boke$ cat test.py #/usr/bin/env python #coding:utf-8 import re with open('text','r') as txt: f = txt.read() print re.match('bin',f) print re.search('bin',f).end() lovelinux@LoveLinux:~/py/boke$ python test.py None 34 lovelinux@LoveLinux:~/py/boke$ vim test.py lovelinux@LoveLinux:~/py/boke$ python test.py None <_sre.SRE_Match object at 0x7f12fc9f9ed0>
返回是对象实例有2个方法,
start():返回记录匹配到字符的开始索引
end():返回记录匹配到字符的结束索引
lovelinux@LoveLinux:~/py/boke$ python test.py None 31 34 lovelinux@LoveLinux:~/py/boke$ cat test.py #/usr/bin/env python #coding:utf-8 import re with open('text','r') as txt: f = txt.read() print re.match('bin',f) print re.search('bin',f).start() print re.search('bin',f).end()

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
