簡體   English   中英

如何使用正則表達式從字符串中提取子字符串

[英]How to extract a substring from a string using regex

我有一個像下面這樣的字符串,如果可能的話,我想使用正則表達式或任何其他方式從該字符串中提取突出顯示的部分

密爾沃基/沙利文的國家氣象局發布了\\n\\n* Tornado Warning for...\\nNorthwestern Columbia County in south central Wisconsin...\\nSouthwestern Marquette County in south central Wisconsin...\\n\\n* \\n Tornado Warning for...\\nNorthwestern Columbia County in south central Wisconsin...\\nSouthwestern Marquette County in south central Wisconsin...\\n\\n*直到CDT 晚上 945 點。\\n\\n* CDT 下午 911 點,一場能夠產生龍卷風的強雷暴\\n位於威斯康星戴爾以東 8 英里處,以 45\\nmph 的速度向東北移動。\\n\\n危險...龍卷風。\\n \\n來源...雷達指示旋轉。\\n\\n影響...飛行碎片對於那些沒有\\n庇護所捕獲的人來說是危險的。 移動房屋將被損壞或毀壞。\\n屋頂、窗戶和車輛將發生損壞。 樹木\\n可能會損壞。\\n\\n* 受影響的地點包括...\\nPackwaukee、Endeavor 和 Briggsville。

description = 'The National Weather Service in Milwaukee/Sullivan has issued a\n\n* Tornado Warning for...\nNorthwestern Columbia County in south central Wisconsin...\nSouthwestern Marquette County in south central Wisconsin...\n\n* Until 945 PM CDT.\n\n* At 911 PM CDT, a severe thunderstorm capable of producing a tornado\nwas located 8 miles east of Wisconsin Dells, moving northeast at 45\nmph.\n\nHAZARD...Tornado.\n\nSOURCE...Radar indicated rotation.\n\nIMPACT...Flying debris will be dangerous to those caught without\nshelter. Mobile homes will be damaged or destroyed.\nDamage to roofs, windows, and vehicles will occur.  Tree\ndamage is likely.\n\n* Locations impacted include...\nPackwaukee, Endeavor and Briggsville.'

#now I want to match substring between (Tornado Warning for... *** ...\n\n*)

# I tried to like this

re.search('Tornado Warning for...(.*)\n\n*', description)

# I am getting results like this

<re.Match object; span=(67, 90), match='Tornado Warning for...\n'>

#expected result 

<re.Match object; span=(any, any), match='Tornado Warning for...\nNorthwestern Columbia County in south central Wisconsin...\nSouthwestern Marquette County in south central Wisconsin...\n\n*'>

它不匹配完​​整子字符串,它唯一匹配Tornado Warning for...\\n

我想匹配Tornado Warning for...\\nNorthwestern Columbia County in south central Wisconsin...\\nSouthwestern Marquette County in south central Wisconsin...\\n\\n*

其中 substring 開始Tornado Warning for...並結束\\n\\n*

感謝您的幫助,並為我的英語不好而道歉

你可以匹配

\bTornado Warning for\.\.\.(?:\n.*)*?\n\n

模式匹配:

  • \\bTornado Warning for\\.\\.\\. 匹配Tornado Warning for前面有一個單詞邊界並轉義點以逐字匹配它們
  • (?:\\n.*)*? 匹配盡可能少的換行符和該行的其余部分
  • \\n\\n匹配 2 個換行符

正則表達式演示| Python 演示

例如

import re

description = 'The National Weather Service in Milwaukee/Sullivan has issued a\n\n* Tornado Warning for...\nNorthwestern Columbia County in south central Wisconsin...\nSouthwestern Marquette County in south central Wisconsin...\n\n* Until 945 PM CDT.\n\n* At 911 PM CDT, a severe thunderstorm capable of producing a tornado\nwas located 8 miles east of Wisconsin Dells, moving northeast at 45\nmph.\n\nHAZARD...Tornado.\n\nSOURCE...Radar indicated rotation.\n\nIMPACT...Flying debris will be dangerous to those caught without\nshelter. Mobile homes will be damaged or destroyed.\nDamage to roofs, windows, and vehicles will occur.  Tree\ndamage is likely.\n\n* Locations impacted include...\nPackwaukee, Endeavor and Briggsville.'

m = re.search(r'\bTornado Warning for\.\.\.(?:\n.*)*?\n\n', description)
if m:
    print(m.group())

輸出

Tornado Warning for...
Northwestern Columbia County in south central Wisconsin...
Southwestern Marquette County in south central Wisconsin...

正則表達式可能如下所示:

matched_string = re.findall("Tornado[a-zA-Z\s\.\\\*]+\\n\\n\*", description)
print(matched_string)

. 無法匹配\\n 改用[\\W\\w] .

import re
description = 'The National Weather Service in Milwaukee/Sullivan has issued a\n\n* Tornado Warning for...\nNorthwestern Columbia County in south central Wisconsin...\nSouthwestern Marquette County in south central Wisconsin...\n\n* Until 945 PM CDT.\n\n* At 911 PM CDT, a severe thunderstorm capable of producing a tornado\nwas located 8 miles east of Wisconsin Dells, moving northeast at 45\nmph.\n\nHAZARD...Tornado.\n\nSOURCE...Radar indicated rotation.\n\nIMPACT...Flying debris will be dangerous to those caught without\nshelter. Mobile homes will be damaged or destroyed.\nDamage to roofs, windows, and vehicles will occur.  Tree\ndamage is likely.\n\n* Locations impacted include...\nPackwaukee, Endeavor and Briggsville.'

print(re.search(r'Tornado Warning for\.\.\.([\W\w]*?)\n\n\*', description).group())

"""
Tornado Warning for...
Northwestern Columbia County in south central Wisconsin...
Southwestern Marquette County in south central Wisconsin...

*
"""

暫無
暫無

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

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