A. python正则表达式的几种匹配方法
1.测试正则表达式是否匹配字符串的全部或部分
regex=ur"" #正则表达式
if re.search(regex, subject):
do_something()
else:
do_anotherthing()
2.测试正则表达式是否匹配整个字符串
regex=ur"/Z" #正则表达式末尾以/Z结束
if re.match(regex, subject):
do_something()
else:
do_anotherthing()
3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
# match start: match.start()
# match end (exclusive): atch.end()
# matched text: match.group()
do_something()
else:
do_anotherthing()
4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
result = match.group()
else:
result = ""
B. python 判断正则表达式
看了你的提问,你的要求是:
输入格式:
输入包含两行:
待匹配字符串
正则表达式
输出格式:
若正则表达式能够匹配第一行字符串则输出True,否则,输出False
以下是我依据你的功能需求,个人简单写的一些代码,供你参考:
importre
flg=True
#定义主要工作代码函数
defjobCode(txtstr,regex):
result=re.search(regex,txtstr)
#如果匹配第一行字符串flg为True,否则flg为False
ifresult.group()==txtstr:
#print(result.group())
returnflg==True#返回flg并终止循环
else:
#print(result.group())
returnflg==False#返回flg并终止循环
#程序主入口
if__name__=='__main__':
txtstr=str(input("请输入待匹配的字符串:"))
regex=input("请输入正则表达式:")
print(jobCode(txtstr,regex))#调用定义函数jobCode()
代码应该还能更简洁,具体你自己去完善。
纯手工,如果对你有帮助望采纳!
C. 求python 正则表达式, regex 判断是否存在abc.abc.abc
importre
text='''<android.support.v7.internal.widget.ActionBarContextViewandroid:ellipsize="android.support.v7.internal.widget.ActionBarContextView"android:id="@id/action_bar_subtitle"android:visibility="gone"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="@dimen/abc_action_bar_subtitle_top_margin_material"android:singleLine="true"/>'''
htm=re.findall(r"w+.w+.w+.w+.w+.w+",text)
print(htm)
如果不确定小数点个数
importre
text='''<android.support.v7.internal.widget.ActionBarContextViewandroid:ellipsize="android.support.v7.internal.widget.ActionBarContextView"android:id="@id/action_bar_subtitle"android:visibility="gone"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="@dimen/abc_action_bar_subtitle_top_margin_material"android:singleLine="true"/>'''
htm=re.findall(r"((w+.)+w+)",text)
fortinhtm:
print(t[0])
D. python的正则表达式怎么用
在ipython中测试一下代码:(读入一个图片文件的地址字符串)
?
1
2
3
4
5
6
7
8
9
10
11
12
a = input("input a:\n")
print "the input method: ",a
b = raw_input("input b:\n")
print "the raw_input method: ",b
input a:
'/home/sunny/caffe-master/examples/images/cat.jpg'
the input method: /home/sunny/caffe-master/examples/images/cat.jpg
input b:
'/home/sunny/caffe-master/examples/images/cat.jpg'
the raw_input method: '/home/sunny/caffe-master/examples/images/cat.jpg'
另外,对于两种输入方式另一个直观区别就是input自带运算处理功能,也就是输入算式的话会直接输出结果,而raw_input会原汁原味(raw)地输出:
?
1
2
3
4
#! -*- coding:utf-8 -*-
print raw_input(u'测试raw_input:\n')
print input(u'测试input:\n')
E. python正则表达式,匹配开头和结尾获取字符串
importre
A='''/22Q1006NOSIG=<BR/>/23Q1007NOSIG=<BR/>/22Q1006NOSIG=<BR/>'''
reg=re.findall(r'(?:METAR|SPECI)+[^=]+=',A)
print(reg[0])
F. Python正则表达式的几种匹配用法
下面列出: 1.测试正则表达式是否匹配字符串的全部或部分regex=ur"" #正则表达式
if re.search(regex, subject): do_something()else: do_anotherthing() 2.测试正则表达式是否匹配整个字符串 regex=ur"/Z" #正则表达式末尾以/Z结束
if re.match(regex, subject): do_something()else: do_anotherthing() 3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string) regex=ur"" #正则表达式
match = re.search(regex, subject)if match: # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() do_something()else: do_anotherthing() 4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex) regex=ur"" #正则表达式
match = re.search(regex, subject)if match: result = match.group()else: result ="" 5. 获取捕获组所匹配的子串(Get the part of a string matched by a capturing group) regex=ur"" #正则表达式
match = re.search(regex, subject)if match: result = match.group(1)else: result ="" 6. 获取有名组所匹配的子串(Get the part of a string matched by a named group) regex=ur"" #正则表达式
match = re.search(regex, subject)if match:result = match.group"groupname")else:result = "" 7. 将字符串中所有匹配的子串放入数组中(Get an array of all regex matches in a string) result = re.findall(regex, subject) 8.遍历所有匹配的子串(Iterate over all matches in a string) for match in re.finditer(r"<(.*?)/s*.*?//1>", subject) # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() 9.通过正则表达式字符串创建一个正则表达式对象(Create an object to use the same regex for many operations) reobj = re.compile(regex) 10.用法1的正则表达式对象版本(use regex object for if/else branch whether (part of) a string can be matched) reobj = re.compile(regex)if reobj.search(subject): do_something()else: do_anotherthing() 11.用法2的正则表达式对象版本(use regex object for if/else branch whether a string can be matched entirely) reobj = re.compile(r"/Z") #正则表达式末尾以/Z 结束
if reobj.match(subject): do_something()else: do_anotherthing() 12.创建一个正则表达式对象,然后通过该对象获得匹配细节(Create an object with details about how the regex object matches (part of) a string) reobj = re.compile(regex) match = reobj.search(subject)if match: # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() do_something()else: do_anotherthing() 13.用正则表达式对象获取匹配子串(Use regex object to get the part of a string matched by the regex) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group()else: result ="" 14.用正则表达式对象获取捕获组所匹配的子串(Use regex object to get the part of a string matched by a capturing group) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group(1)else: result ="" 15.用正则表达式对象获取有名组所匹配的子串(Use regex object to get the part of a string matched by a named group) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group("groupname")else: result ="" 16.用正则表达式对象获取所有匹配子串并放入数组(Use regex object to get an array of all regex matches in a string) reobj = re.compile(regex) result = reobj.findall(subject) 17.通过正则表达式对象遍历所有匹配子串(Use regex object to iterate over all matches in a string) reobj = re.compile(regex)for match in reobj.finditer(subject): # match start: match.start() # match end (exclusive): match.end() # matched text: match.group()字符串替换 1.替换所有匹配的子串 #用newstring替换subject中所有与正则表达式regex匹配的子串
result = re.sub(regex, newstring, subject) 2.替换所有匹配的子串(使用正则表达式对象) reobj = re.compile(regex) result = reobj.sub(newstring, subject) 字符串拆分 1.字符串拆分 result = re.split(regex, subject) 2.字符串拆分(使用正则表示式对象) reobj = re.compile(regex) result = reobj.split(subject)
G. 正则(=.*[a-z])是什么意思
.代表任意字符,(?=.*[a-z]) 表示任意字符拼接小写字母,那(?=[a-z])跟它有什么区别?
首先(?=xxx) 是 look ahead=你说的 前瞻
此处xxx的区别:
.*[a-z]:任意字符,且个数尽量多,后跟单个小写字母
举例:12AB3c4d
[a-z]:单个小写字母
举例:12AB3c4d 只能匹配到:12AB3c
12AB3是前面部分
c是最后的[a-z]匹配的
-》其实,更准确的解释需要你给出完整的正则,而不仅仅是look ahead的部分,才能详细和精确解释匹配的内容和逻辑。
前瞻:exp1(?=exp2) 查找exp2前面的exp1,
后顾:(?<=exp2)exp1 查找exp2后面的exp1,
(?=.*[a-z]). 和 .(?=.*[a-z]) 我测试都能运行,反而(?<=.*[a-z]). 运行不了,是不是后顾表达式不能用 (?<=exp2)exp1 了?
不是。而是:
look behind=后顾 有个特殊的情况:只支持固定长度,比如123,而不支持不定长度的,比如d+
你的(?<=.*[a-z]). 中的.*[a-z] ,属于正则写法,能匹配到不固定长度的字符,所以不支持,会报错的。
官网解释,详见python中的re
re --- 正则表达式操作 — Python 3.8.1 文档
(?<=…)
匹配字符串的当前位置,它的前面匹配…的内容到当前位置。这叫:dfn:positive lookbehind assertion(正向后视断定)。(?<=abc)def会在'abcdef'中找到一个匹配,因为后视会往后看3个字符并检查是否包含匹配的样式。包含的匹配样式必须是定长的,意思就是abc或a|b是允许的,但是a*和a{3,4}不可以。
注意其中的“包含的匹配样式必须是定长的”
用菜鸟工具正则表达式在线测试,abcD去匹配 (?=.*[a-z]). 匹配结果是 a b c,为什么a也能匹配,a的前面什么都没有啊!
建议换用更好用的
RegExr: Learn, Build, & Test RegEx
H. python 正则表达式是什么
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。
正则表达式是用来匹配字符串非常强大的工具,在其他编程语言中同样有正则表达式的概念,Python同样不例外,利用了正则表达式,我们想要从返回的页面内容提取出我们想要的内容就易如反掌了。
正则表达式的大致匹配过程是:
1、依次拿出表达式和文本中的字符比较。
2、如果每一个字符都能匹配,则匹配成功;一旦有匹配不成功的字符则匹配失败。
3、如果表达式中有量词或边界,这个过程会稍微有一些不同。
I. python 正则表达式,怎样匹配以某个字符串开头,以某个字符串结尾的情况
python正则匹配以xx开头以xx结尾的单词的步骤:
1、假设需要匹配的字符串为:site sea sue sweet see case sse ssee loses需要匹配的为以s开头以e结尾的单词。正确的正则式为:sS*?e
2、使用python中re.findall函数表示匹配字符串中所有的可能选项,re是python里的正则表达式模块。findall是其中一个方法,用来按照提供的正则表达式,去匹配文本中的所有符合条件的字符串。
3、代码和结果如下:
text ='site sea sue sweet see case sse ssee loses'
re.findall(r'sS*?e',text)
结果为:['site', 'sue', 'see', 'sse', 'ssee']
(9)python正则表达式在线测试扩展阅读:
python正则匹配,以某某开头某某结尾的最长子串匹配
代码如下:
regVersions = re.search(r'(V|v)[0-9].*[0-9]', filename)
if regVersions:
print regVersions.group()
J. python正则表达式是什么呢
python正则表达式如下:
在python中,所谓的“正则表达式”指的是通常被用来检索、替换那些符合某个模式的一段文本。具体而言,它的作用是检测某个字符串是否符合规则和提取网页字符串中想要的数据。
正则表达式是对字符串提取的一套规则,我们把这个规则用正则里面的特定语法表达出来,去匹配满足这个规则的字符串。正则表达式具有通用型,不仅python里面可以用,其他的语言也一样适用。
python的编程特点:
速度快:Python的底层是用C语言写的,很多标准库和第三方库也都是用C写的,运行速度非常快。
免费、开源:Python是FLOSS(自由/开放源码软件)之一。使用者可以自由地发布这个软件的拷贝、阅读它的源代码、对它做改动、把它的一部分用于新的自由软件中。FLOSS是基于一个团体分享知识的概念。
高层语言:用Python语言编写程序的时候无需考虑诸如如何管理你的程序使用的内存一类的底层细节。
解释性:一个用编译性语言比如C或C++写的程序可以从源文件(即C或C++语言)转换到一个你的计算机使用的语言(二进制代码,即0和1)。这个过程通过编译器和不同的标记、选项完成。