python正则表达式使用实例 he c

请问这个python正则表达式是什么意思matches = re.findall(’([^\s\\\]+)’, line)

这个是用来匹配不带空格和反斜杠的字符串。
这里的^匹配非中括号里的字符;\s代表空格;\\\代表字符反斜杠(正则表达式需要双反斜杠来转义,因为\在正则表达式中本身就具有含义)
line = ’haha \\\\ \\ hehe’
matches = re.findall(’([^\s\\\]+)’, line)
print matches # [’haha’, ’hehe’]
望采纳!

python正则表达式是什么呢

python正则表达式如下:

在python中,所谓的“正则表达式”指的是通常被用来检索、替换那些符合某个模式的一段文本。具体而言,它的作用是检测某个字符串是否符合规则和提取网页字符串中想要的数据。

正则表达式是对字符串提取的一套规则,我们把这个规则用正则里面的特定语法表达出来,去匹配满足这个规则的字符串。正则表达式具有通用型,不仅python里面可以用,其他的语言也一样适用。

python的编程特点:

速度快:Python的底层是用C语言写的,很多标准库和第三方库也都是用C写的,运行速度非常快。

免费、开源:Python是FLOSS(自由/开放源码软件)之一。使用者可以自由地发布这个软件的拷贝、阅读它的源代码、对它做改动、把它的一部分用于新的自由软件中。FLOSS是基于一个团体分享知识的概念。

高层语言:用Python语言编写程序的时候无需考虑诸如如何管理你的程序使用的内存一类的底层细节。

解释性:一个用编译性语言比如C或C++写的程序可以从源文件(即C或C++语言)转换到一个你的计算机使用的语言(二进制代码,即0和1)。这个过程通过编译器和不同的标记、选项完成。

python怎么根据正则表达式提取指定的内容

python 根据正则表达式提取指定的内容
正则表达式是极其强大的,利用正则表达式来提取想要的内容是很方便的事。
下面演示了在python里,通过正则表达式来提取符合要求的内容。
实例代码:
import re#   正则表达式是极其强大的,利用正则表达式来提取想要的内容是很方便的事。# 下面演示了在python里,通过正则表达式来提取符合要求的内容。有几个要注意# 的地方就是:# 要用()将需要的内容包含起来# 编号为0的group是整个符合正则表达式的内容,编号为1的是第一个(及对应# 的)包含的内容# @param regex: regular expression, use () to group the result# 正则表达式,用()将要提取的内容包含起来# @param content: # @param index: start from 1, depends on the \p regex’s ()# 从1开始,可以通过数(来得到,其中0是全部匹配# @return: the first match of the \p regex# 只返回第一次匹配的内容def extractData(regex, content, index=1): r = ’0’ p = re.compile(regex) m = p.search(content) if m: r = m.group(index) return r regex = r’第(.*)场雪’content = ’2002年的第一场雪’index = 1print extractData(regex, content, index)

如何用python正则

(1)re.match()函数

re.match 尝试从字符串的开始匹配一个模式。

函数语法:

re.match(pattern, string, flags=0)

函数参数说明:

参数

描述

pattern    匹配的正则表达式    

string    要匹配的字符串。    

flags    标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。    

匹配成功re.match方法返回一个匹配的对象,否则返回None。

我们可以使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。

匹配对象方法

描述

group(num=0)    匹配的整个表达式的字符串,group() 可以一次输入多个组号,在这种情况下它将返回一个包含那些组所对应值的元组。    

groups()    返回一个包含所有小组字符串的元组,从 1 到 所含的小组号。    

(2)re.research()函数

re.search匹配整个字符串,直到找到一个匹配。

函数语法:

re.search(pattern, string, flags=0)

函数参数说明:

参数

描述

pattern    匹配的正则表达式    

string    要匹配的字符串。    

flags    标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。    

匹配成功re.search方法方法返回一个匹配的对象,否则返回None。

我们可以使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。

匹配对象方法

描述

group(num=0)    匹配的整个表达式的字符串,group() 可以一次输入多个组号,在这种情况下它将返回一个包含那些组所对应值的元组。    

groups()    返回一个包含所有小组字符串的元组,从 1 到 所含的小组号。    

(3)实例

我这里采用re.search()函数解决的问题。

测试数据集为购物网站用户评论

实例代码:

[python] view plain copy

  • # -*-coding:utf-8 -*-  

  • import sys  

  • import re  

  • reload(sys)  

  • text = open(’JD_DFB_comments.txt’)  

  • line = text.readline()  

  • #i = 0   

  • while line:  

  • #i = i+1  

  • #re.search匹配整个字符串,直到找到一个匹配。  

  • n1 = re.search(r’(要(是|能)(.*)就(更|好|再|直观|完美|太)(.*)了)’,line)  

  • n2 = re.search(r’(如果)(.*)就(更|好|再|直观|完美|太)(.*)了’,line)  

  • #打开将要写入的数据  

  • data = open(’aa.txt’,’a’)  

  • if n1:  

  • #print line  

  • data.write(line)      #写入匹配到的数据  

  • #print i              记录匹配结果所在的行数  

  • #print n1.group()     #等于print line  

  • print n1.group(3)     #打出第三个括号里的内容  

  • if n2:  

  • #print n2.group()  

  • print n2.group(2)          

  • line = text.readline()  

  • text.close()  

  • 阅读更多

个人分类: Python语言

想对作者说点什么? 我来说一句

Python中re的match、search、findall、finditer区别

python 使用正则表达式 匹配“非长字符串”

在我们日常使用中,经常需要搜索关键位置进行字符串的匹配,比如一行文本的开头,又比如一个字符串的开头,或者结尾。 这时候就需要使用正则表达式的边界符进行匹配,它们定义如下:
定义字符 意义
^ 字符串的开头或一行的开头
$ 字符串的结尾或一行的结尾
\A 字符串的开头
\Z 字符串的结尾
\b 空字符串的开头或一个单词的结尾
\B 非空字符串的开头或非一个单词的结尾,与\b相反
测试例子如下:
#python 3.6#http://blog.csdn.net/caimouse/article/details/51749579#from re_test_patterns import test_patternstest_patterns(’This is some text — with punctuation.’,[(r’^\w+’, ’word at start of string’),(r’\A\w+’, ’word at start of string’),(r’\w+\S*$’, ’word near end of string’),(r’\w+\S*\Z’, ’word near end of string’),(r’\w*t\w*’, ’word containing t’),(r’\bt\w+’, ’t at start of word’),(r’\w+t\b’, ’t at end of word’),(r’\Bt\B’, ’t, not start or end of word’)],)

python中如何使用正则表达式的非贪婪模式示例

import re
str=’abcdxyzsd1232abc’
regex_greed=’a.*d’  # 贪婪模式,a开头找到最后的一个d才结束
match_greed=re.match(regex_greed,str)
print match_greed.group()     # 匹配结果:abcdxyzsd

regex_not_greed=’a.*?d’  # 非贪婪模式,a开头找到第一个d就结束
match_not_greed=re.match(regex_not_greed,str)
print match_not_greed.group()    # 匹配结果:abcd

Python正则表达式match和search区别,举个例子

re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。

re.search 扫描整个字符串并返回第一个成功的匹配。

re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。

实例:

import re
line = “Cats are smarter than dogs“; 
matchObj = re.match( r’dogs’, line, re.M|re.I)
if matchObj:
    print(“match –》 matchObj.group() : “, matchObj.group())
else:
    print(“No match!!“)
matchObj = re.search( r’dogs’, line, re.M|re.I)
if matchObj:
    print(“search –》 matchObj.group() : “, matchObj.group()
else:
    print(“No match!!“)

运行结果:

No match!!
search –》 matchObj.group() :  dogs

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)