python 使用正则表达式匹配python中文件的开头和结尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2362471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Match start and end of file in python with regex
提问by zlack
I'm having a hard time finding the regex for the start and end of a file in python. How would I accomplish this ?
我很难在 python 中找到文件开头和结尾的正则表达式。我将如何做到这一点?
回答by Mark Tolonen
Read the whole file into a string, then \A matches only the beginning of a string, and \Z matches only the end of a string. With re.MULTILINE, '^' matches the beginning of the string andthe just after a newline, and '$' matches the end of the string andjust before a newline. See the Python documentation for re syntax.
将整个文件读入一个字符串,然后\A 只匹配字符串的开头,而\Z 只匹配字符串的结尾。使用 re.MULTILINE,'^' 匹配字符串的开头和换行符之后,'$' 匹配字符串的结尾和换行符之前。有关re 语法,请参阅 Python 文档。
import re
data = '''sentence one.
sentence two.
a bad sentence
sentence three.
sentence four.'''
# find lines ending in a period
print re.findall(r'^.*\.$',data,re.MULTILINE)
# match if the first line ends in a period
print re.findall(r'\A^.*\.$',data,re.MULTILINE)
# match if the last line ends in a period.
print re.findall(r'^.*\.$\Z',data,re.MULTILINE)
Output:
输出:
['sentence one.', 'sentence two.', 'sentence three.', 'sentence four.']
['sentence one.']
['sentence four.']
回答by ghostdog74
Maybe you should pose your question more clearly, like what you trying to do. That said, you can slurp the file into one whole string, and match your pattern using re.
也许你应该更清楚地提出你的问题,比如你想做什么。也就是说,您可以将文件放入一个完整的字符串中,然后使用 re 匹配您的模式。
import re
data=open("file").read()
pat=re.compile("^.*pattern.*$",re.M|re.DOTALL)
print pat.findall(data)
There are better ways to do what you want, whatever it is, without re.
有更好的方法可以做你想做的事,无论是什么,而无需重新。
回答by John Machin
regex $
is NOTyour friend; see this SO answer
正则表达式$
是不是你的朋友; 看到这个答案