Python Regular Expression
2015-09-14 19:53
465 查看
Regular expression in Python
Greedy Matching
Output:
['From: Using the :']
Output:
['From: Using the :']
NON-Greedy Matching
Output:
['From:']
Output:
['From:']
说明:在字符串'From:Using the :character' 中, 有两个: 号存在。 当要匹配以F开头,以: 号结尾的字符串时,GreedyMatching 会以Greedy 为原则找到尽可能长的匹配字符串。 而NON-Greedy Matching 不会像Greedy 那样去找尽可能长的串。
^ | Matches the beginning of a line |
$ | Matches the end of the line |
. | Matches any character |
\s | matches whitespace |
\S | matches any non-whitespace |
* | repeats a character 0 or more times |
*? | repeats a character 0 or more times(non-greedy) |
+ | repeats a character 1 or more times |
+? | repeats a character 1 or more times(non-greedy) |
[aeiou] | matches a single character in the listed set |
[^XYZ] | matches a single character not in the listed set |
[a-z0-9] | the set of characters can include a range |
( | indicates where string extraction is to start |
) | indicates where string extraction is to end |
import re x='From: Using the :character' y=re.findall('^F.+:', x) print (y) |
['From: Using the :']
import re x='From: Using the :character' y=re.findall('^F.*:', x) print (y) |
['From: Using the :']
NON-Greedy Matching
import re x='From: Using the :character' y=re.findall('^F.+?:', x) print (y) |
['From:']
import re x='From: Using the :character' y=re.findall('^F.*:', x) print (y) |
['From:']
说明:在字符串'From:Using the :character' 中, 有两个: 号存在。 当要匹配以F开头,以: 号结尾的字符串时,GreedyMatching 会以Greedy 为原则找到尽可能长的匹配字符串。 而NON-Greedy Matching 不会像Greedy 那样去找尽可能长的串。
相关文章推荐
- python核心编程第2章课后题答案(第二版36页)
- python_docx
- 为什么python在函数中修改"字典型全局变量"不需要global
- 6 Easy Steps to Learn Naive Bayes Algorithm (with code in Python)
- Hive中使用python
- 创Python规划2
- python图片简易二值化
- Python 基础编程
- Python 基础编程
- python文件输入和输出
- local variable 'xxx' referenced before assignment----引申python作用域
- 【python】 random模块
- python——简单爬虫
- Numpy中矩阵对象(matrix)
- Python正则判断手机和邮箱
- python代码小记
- 零基础学Google Freebase API(python版)
- Python执行系统命令的方法 os.system(),os.popen(),commands
- python3 编码问题
- Python习题练习01