我想申請正則表達式的文件的內容,而將整個文件加載到一個字符串。 RegexObject將第一個參數作爲字符串或緩衝區。有沒有辦法將文件轉換成緩衝區?如何將正則表達式應用於文件的內容?
2
A
回答
2
報價:
緩衝區對象不是直接通過 Python語法支持,但可以 通過調用內置 功能緩衝()創建的。
和其他一些有趣的部分:
緩衝液(對象[,偏移,大小]])
對象參數必須是 支持緩衝器呼叫接口的對象 (如字符串,數組和緩衝區)
名File對象沒有實現緩衝界面 - 讓你不得不改變其內容要麼轉換爲字符串(f.read()
)或成陣列(使用mmap爲該)。
4
1
搜索讀入行的文件在一個時間並應用REG EXP到該行。似乎被堆疊起來處理字符串。 http://docs.python.org/library/re.html包含更多的細節,但我無法找到有關緩衝區的任何內容。從Python的文檔
+0
唯一的問題是如果正則表達式匹配跨行(`/ foo \ nbar /`)... – ircmaxell 2011-02-07 20:00:26
0
進行緩衝自己。如果正則表達式匹配塊的一部分,則從該塊中刪除該部分,繼續使用未使用的部分,讀取下一個塊,重複。
如果正則表達式被設計爲一個特定的理論最大的,對什麼都不匹配,緩衝是在執法機關一樣大的情況下,清除緩衝區,在接下來的塊讀取。一般來說,正則表達式不是用來處理非常大的數據塊的。正則表達式越複雜,它所做的回溯越多。
0
下面的代碼演示:
- 打開文件
- 文件
- 在求只讀取文件
- 使用正則表達式匹配的模式
的一部分假設:所有的句子是個Ë相同長度
# import random for randomly choosing in a list
import random
# import re for regular expression matching
import re
#open a new file for read/writing
file = open("TEST", "r+")
# some strings to put in the sentence
typesOfSentences = ["test", "flop", "bork", "flat", "pork"]
# number of types of sentences
numTypes = len(typesOfSentences)
# for i values 0 to 99
for i in range(100):
# Create a random sentence for example
# "This is a test sentence 01"
sentence = "This is a %s sentence %02d\n" % (random.choice(typesOfSentences), i)
# write the sentence to the file
file.write(sentence)
# Go back to beginning of file
file.seek(0)
# print out the whole file
for line in file:
print line
# Determine the length of the sentence
length = len(sentence)
# go to 20th sentence from the beginning
file.seek(length * 20)
# create a regex matching the type and the number at the end
pathPattern = re.compile("This is a (.*?) sentence (\d\d)")
# print the next ten types and numbers
for i in range(10):
# read the next line
line = file.readline()
# match the regex
match = pathPattern.match(line)
# if there was a match
if match:
# NOTE: match.group(0) is always the entire sentence
# Print type of sentence it was and it's number
print "Sentence %02d is of type %s" % (int(match.group(2)), match.group(1))
相關問題
- 1. 如何基於正則表達式將文件內容分爲不同的組?
- 2. 使用正則表達式將文件夾內容寫入CSV
- 3. 的Javascript正則表達式 - 如何基於正則表達式
- 4. Ant正則表達式替換文件內容的模式
- 5. 正則表達式的內容
- 6. 恢復內容的正則表達式
- 7. 如何使用正則表達式獲取內部html內容
- 8. 如何將正則表達式應用於多行?
- 9. 作爲grep的正則表達式的文件內容
- 10. 正則表達式用於匹配括號之間的內容
- 11. 正則表達式適用於html外的大括號內容
- 12. java在文件內容中的正則表達式
- 13. 在文件中匹配正則表達式的內容?
- 14. 如何使用jQuery解析正則表達式的內容?
- 15. 如何使用正則表達式檢索DIV的內容?
- 16. 內容URL正則表達式
- 17. 正則表達式提及HTML內容
- 18. 正則表達式:HTML,內容
- 19. 如何應用此正則表達式?
- 20. 如何使用PHP正則表達式查找內容
- 21. 如何在純ANT文件上應用正則表達式1.8
- 22. 將一組連續的正則表達式應用於多個文件(emacs)
- 23. 更換表達的內容在C++ [的std ::正則表達式]
- 24. 如何將文件的內容插入到另一個文件(如果正則表達式)在Perl /殼
- 25. 使用正則表達式分隔文本文件內容的分隔符?
- 26. 用於格式化文件的正則表達式
- 27. SPARQL正則表達式不顯示應該顯示的內容
- 28. 如何使用正則表達式選擇多個不同的文本/內容
- 29. 什麼是正則表達式以及如何使用正則表達式加載html內容android
- 30. 用於提取文本文件名稱的正則表達式
你試圖將正則表達式應用到整個文件 - 我。e試圖將整個文件與您的正則表達式匹配 - 或者您是否試圖逐行匹配文件或以其他一些大小的塊進行匹配? – 2011-02-07 19:29:05