簡體   English   中英

正則表達式在 \n 處拆分字符串,但如果是 \n\n 則跳過第一個

[英]Regex split the string at \n but skip the first one if it is \n\n

我想通過在 \n 處分隔來拆分 Python 上的一些字符串並以該格式使用它們,但是其中一些字符串具有意外的換行符,我想忽略它們。

澄清:兩個例子只有一個字符串。

例如,這是一個沒有意外換行符的常規字符串:

Step 1
Cut peppers into strips.
Step 2
Heat a non-stick skillet over medium-high heat. Add peppers and cook on stove top for about 5 minutes.
Step 3
Toast the wheat bread and then spread hummus, flax seeds, and spinach on top
Step 4
Lastly add the peppers. Enjoy!

但其中一些是這樣的:

Step 1
Using a fork, mash up the tuna really well until the consistency is even.

Step 2
Mix in the avocado until smooth.

Step 3
Add salt and pepper to taste. Enjoy!

我不得不說我是正則表達式的新手,如果解決方案很明顯,請原諒

編輯:這是我的正則表達式

    stepOrder = []
    # STEPS
    txtSteps = re.split("\n",directions.text)
    listOfLists = [[] for i in range(len(txtSteps)) if i % 2 == 0]
    for i in range(len(listOfLists)):
        listOfLists[i] = [txtSteps[i*2],txtSteps[i*2+1]]
    recipe["steps"] = listOfLists
    print(listOfLists)

direction.text 是我給出的每一個例子。 我也可以分享它是什么,但我認為這無關緊要。

f = open("your_file_name")
content = f.read()
f.close()

for line in content.split("\n"):
    if re.match("^&",line):
        continue
    print(line)

您可以通過拆分以下正則表達式來解決此問題:

(?<=\d\n).*

基本上它會在同一行中獲得任何字符.* ,其前面是一位數字\d和一個換行符\n

此處查看正則表達式演示。


然后使用re.findall方法將整個 Python 片段簡化如下:

# STEPS
steps = re.findall("(?<=\d\n).*", directions.text)
out = [[{'order':i+1, 'step': step}] for i, step in enumerate(steps)]

暫無
暫無

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

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