您的位置:首页 > 编程语言 > Python开发

Python Regular Expression

2015-09-14 19:53 465 查看
Regular expression in Python

^
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
Greedy Matching

import re

x='From: Using the :character'

y=re.findall('^F.+:', x)

print (y)

Output:

['From: Using the :']

import re

x='From: Using the :character'

y=re.findall('^F.*:', x)

print (y)

Output:

['From: Using the :']

NON-Greedy Matching

import re

x='From: Using the :character'

y=re.findall('^F.+?:', x)

print (y)

Output:

['From:']

import re

x='From: Using the :character'

y=re.findall('^F.*:', x)

print (y)

Output:

['From:']

说明:在字符串'From:Using the :character' 中, 有两个: 号存在。 当要匹配以F开头,以: 号结尾的字符串时,GreedyMatching 会以Greedy 为原则找到尽可能长的匹配字符串。 而NON-Greedy Matching 不会像Greedy 那样去找尽可能长的串。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: