Python Regular Expression

来源:互联网 发布:阿里云 储值卡 余额 编辑:程序博客网 时间:2024/05/26 12:56

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 那样去找尽可能长的串。


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 十个月宝宝不爱吃饭怎么办 十个月宝宝突然不爱吃饭怎么办 二十个月宝宝不爱吃饭怎么办 十个月的宝宝不爱吃饭怎么办 6年级学生数学差怎么办 打印机打不出来就是一张白纸怎么办 wps表格下拉数字不递增怎么办 wps表格圈怎么打出来怎么办 手表固定圈掉了怎么办 起来觉得头晕头胀怎么办? 孩子不好好写作业怎么办 孩子考试考差了怎么办 孩子计算题马虎大意怎么办 二年级孩子不认字怎么办 发现计算上的错误怎么办 孩子不好好做作业怎么办 手破了红肿了怎么办呢 老师反应孩子在校粗心胆小怎么办 四年级的学生计算粗心怎么办 老打孩子骂孩子怎么办 站久了脚肿了怎么办 孩子初中了书写越来越潦草怎么办 给孩子自由孩子无法无天怎么办 孩子挑食幼儿园老师该怎么办 老师说孩子挑食家长怎么办 工作中老是粗心不细心怎么办 小孩数学总是特别粗心该怎么办 孩子起范疙瘩的怎么办 做题马虎不认真怎么办 孩子考差了家长怎么办 小孩写作业不认真怎么办 小孩不认真检查作业怎么办 一年级的小孩作业不认真怎么办 一年级学生做题粗心怎么办 一年级的学生做题粗心怎么办 孩子做作业注意力不集中怎么办 小学三年孩子抄答案怎么办 孩子写作业不认真审题怎么办 一年级小孩审题不认真怎么办 孩子审题不认真马虎怎么办 孩子做作业不认真审题怎么办?