在python中使用正则表达式查找可嵌套字符串组
作者:mrr 时间:2021-10-07 07:02:06
在网上看到一个小需求,需要用正则表达式来处理。原需求如下:
找出文本中包含”因为……所以”的句子,并以两个词为中心对齐输出前后3个字,中间全输出,如果“因为”和“所以”中间还存在“因为”“所以”,也要找出来,另算一行,输出格式为:
行号 前面3个字 *因为* 全部 &所以& 后面3个字(标点符号算一个字)
2 还不是 *因为* 这里好, &所以& 没有人
实现方法如下:
#encoding:utf-8
import os
import re
def getPairStriList(filename):
pairStrList = []
textFile = open(filename, 'r')
pattern = re.compile(u'.{3}\u56e0\u4e3a.*\u6240\u4ee5.{3}') #u'\u56e0\u4e3a和u'\u6240\u4ee5'分别为“因为”和“所以”的utf8码
for line in textFile:
utfLine = line.decode('utf8')
result = pattern.search(utfLine)
while result:
resultStr = result.group()
pairStrList.append(resultStr)
result = pattern.search(resultStr,2,len(resultStr)-2)
#对每个字符串进行格式转换和拼接
for i in range(len(pairStrList)):
pairStrList[i] = pairStrList[i][:3] + pairStrList[i][3:5].replace(u'\u56e0\u4e3a',u' *\u56e0\u4e3a* ',1) + pairStrList[i][5:]
pairStrList[i] = pairStrList[i][:len(pairStrList[i])-5] + pairStrList[i][len(pairStrList[i])-5:].replace(u'\u6240\u4ee5',u' &\u6240\u4ee5& ',1)
pairStrList[i] = str(i+1) + ' ' + pairStrList[i]
return pairStrList
if __name__ == '__main__':
pairStrList = getPairStriList('test.txt')
for str in pairStrList:
print str
PS:下面看下python里使用正则表达式的组嵌套
由于组本身是一个完整的正则表达式,所以可以将组嵌套在其他组中,以构建更复杂的表达式。下面的例子,就是进行组嵌套的例子:
#python 3.6
#蔡军生
#http://blog.csdn.net/caimouse/article/details/51749579
#
import re
def test_patterns(text, patterns):
"""Given source text and a list of patterns, look for
matches for each pattern within the text and print
them to stdout.
"""
# Look for each pattern in the text and print the results
for pattern, desc in patterns:
print('{!r} ({})\n'.format(pattern, desc))
print(' {!r}'.format(text))
for match in re.finditer(pattern, text):
s = match.start()
e = match.end()
prefix = ' ' * (s)
print(
' {}{!r}{} '.format(prefix,
text[s:e],
' ' * (len(text) - e)),
end=' ',
)
print(match.groups())
if match.groupdict():
print('{}{}'.format(
' ' * (len(text) - s),
match.groupdict()),
)
print()
return
例子:
#python 3.6
#蔡军生
#http://blog.csdn.net/caimouse/article/details/51749579
#
from re_test_patterns_groups import test_patterns
test_patterns(
'abbaabbba',
[(r'a((a*)(b*))', 'a followed by 0-n a and 0-n b')],
)
结果输出如下:
'a((a*)(b*))' (a followed by 0-n a and 0-n b)
'abbaabbba'
'abb' ('bb', '', 'bb')
'aabbb' ('abbb', 'a', 'bbb')
'a' ('', '', '')
总结
以上所述是小编给大家介绍的在python中使用正则表达式查找可嵌套字符串组网站的支持!
标签:python,正则表达式,字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Go语言实现的web爬虫实例
2023-07-21 02:35:57
如何导出python安装的所有模块名称和版本号到文件中
2022-07-26 15:38:59
![](https://img.aspxhome.com/file/2023/9/88839_0s.png)
形式追随内容?
2010-03-07 15:55:00
![](https://img.aspxhome.com/file/UploadPic/20103/7/ryan100224_01-77s.jpg)
python调用百度API实现人脸识别
2021-06-11 23:52:23
![](https://img.aspxhome.com/file/2023/2/68472_0s.png)
Python 实现子类获取父类的类成员方法
2022-01-14 00:28:17
读取MySQL的log方法
2011-07-01 12:04:57
使用PHP实现生成HTML静态页面
2023-11-14 11:14:41
AI:如何训练机器学习的模型
2023-01-16 13:18:49
![](https://img.aspxhome.com/file/2023/8/81698_0s.png)
一段ASP的HTTP_REFERER判断代码
2011-03-25 10:41:00
python实现两个一维列表合并成一个二维列表
2023-08-06 12:59:44
django框架实现模板中获取request 的各种信息示例
2023-08-31 05:16:41
![](https://img.aspxhome.com/file/2023/8/61738_0s.jpg)
Python中常见的反爬机制及其破解方法总结
2023-03-05 19:38:42
分支任务:从哪里来,回哪里去
2009-09-04 18:58:00
![](https://img.aspxhome.com/file/UploadPic/20099/4/3819638990_da91f304b0-56s.jpg)
Golang依赖注入工具digo的使用详解
2023-08-27 13:00:43
一篇文章带你学习python的函数与类
2023-10-15 05:04:18
python re.match函数的具体使用
2023-07-31 13:16:28
![](https://img.aspxhome.com/file/2023/7/60727_0s.png)
解决python多行注释引发缩进错误的问题
2022-02-08 04:19:53
Python统计分析模块statistics用法示例
2021-01-20 08:44:16
最简洁的asp多重查询的解决方案
2011-04-15 10:50:00
微信小程序输入多行文本的实战记录
2023-09-10 15:29:51
![](https://img.aspxhome.com/file/2023/1/56311_0s.png)