簡體   English   中英

Python下一個子字符串搜索

[英]Python next substring search

我多次發送帶有前同步碼/后同步碼的消息。 我希望能夠在兩個有效的前/后同步碼之間提取消息。 我當前的代碼是

print(msgfile[msgfile.find(preamble) + len(preamble):msgfile.find(postamble, msgfile.find(preamble))])

問題在於,如果后同步碼已損壞,它將打印第一個有效前同步碼和下一個有效后同步碼之間的所有數據。 接收到的文本文件示例為:

garbagePREAMBLEmessagePOSTcMBLEgarbage
garbagePRdAMBLEmessagePOSTAMBLEgarbage
garbagePREAMBLEmessagePOSTAMBLEgarbage

它會打印

messagePOSTcMBLEgarbage
garbagePRdEAMBLEmessage

但是我真正想要打印的是第三行的消息,因為它同時具有有效的前/后同步碼。 所以我想我想要的是能夠從子字符串的下一個實例中查找和索引。 是否有捷徑可尋?

編輯:我不希望我的數據在漂亮的離散行中。 我只是以這種方式格式化,所以更容易看到

逐行處理:

>>> test = "garbagePREAMBLEmessagePOSTcMBLEgarbage\n"
>>> test += "garbagePRdAMBLEmessagePOSTAMBLEgarbage\n"
>>> test += "garbagePREAMBLEmessagePOSTAMBLEgarbage\n"
>>> for line in test.splitlines():
        if line.find(preamble) != -1 and line.find(postamble) != -1:
            print(line[line.find(preamble) + len(preamble):line.find(postamble)])
import re

lines = ["garbagePREAMBLEmessagePOSTcMBLEgarbage",
        "garbagePRdAMBLEmessagePOSTAMBLEgarbage",
        "garbagePREAMBLEmessagePOSTAMBLEgarbage"]

# you can use regex
my_regex = re.compile("garbagePREAMBLE(.*?)POSTAMBLEgarbage")

# get the match found between the preambles and print it
for line in lines:
    found = re.match(my_regex,line)
    # if there is a match print it
    if found:
        print(found.group(1))

# you can use string slicing
def validate(pre, post, message):
    for line in lines:
        # method would break on a string smaller than both preambles
        if len(line) < len(pre) + len(post):
            print("error line is too small")

        # see if the message fits the pattern
        if line[:len(pre)] == pre and line[-len(post):] == post:
            # print message
            print(line[len(pre):-len(post)])

validate("garbagePREAMBLE","POSTAMBLEgarbage", lines)

所有消息都在一行上嗎? 然后,您可以使用正則表達式來標識帶有有效前同步碼和后同步碼的行:

input_file = open(yourfilename)
import re
pat = re.compile('PREAMBLE(.+)POSTAMBLE')
messages = [pat.search(line).group(1) for line in input_file 
            if pat.search(line)]

print messages

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM