2017-09-15 39 views
-1

有沒有在Python一個很好的辦法做到:獲取匹配件返還元組:提取匹配正則表達式的子字符串的優雅方式?

  • 檢查字符串一組正則表達式
  • 如果是的匹配。

所以基本上我想要一個簡單的方法來進入簡單的解析器/掃描器語法,並簡單地提取一定的結構都匹配(例如,元組)

因此,假設我們在一個字符串國家代碼編碼,城市名稱和索引。我們想提取這個:

input = "123-NEWYORK-[2]" 
grammar = "<country,[0-9]+>-<city,[A-Z]*>-[<index,[0-9]*>" 
res = HOW_TO_DO_THIS(input,grammar) 
if res is None: 
    print("Does not match") 
else 
    (countrycode,city,index) = res 
+2

退房這個哥們https://stackoverflow.com/questions/46239445/get-string-that-was-matched-by-regex/46239491容易(更通用) #46239491 – babygame0ver

+0

你需要使用're'模塊;現在的問題太廣泛了,因爲它基本上要求爲該模塊提供一個教程。 – chepner

回答

2

隨着python3你可以做,注意正則表達式已被修改:

import re 
input = "123-NEWYORK-[2]" 
grammar = r"(?P<country>[0-9]+)-(?P<city>[A-Z]*)-(?P<index>\[[0-9]*\])" 
res = re.findall(grammar, input) 
if not res: 
    print("Does not match") 
else: 
    (countrycode,city,index) = res[0] 
    print(countrycode) 

修改:

  • 正確的正則表達式將是(?P[0-9]+)-(?P[A-Z])-(?P[[0-9]])
  • Python中正則表達式模塊的語法是re.findall(patter, input_string)。不是相反的。
  • if not xif x is None
0

看看這段代碼。這僅僅是簡單的文本查找,但你可以擴展根據您的情況

import re 
f=open('sample.txt',"w") 
f.write("<p class = m>babygameover</p>") 
f.close() 
f=open('sample.txt','r') 
string = "<p class = m>(.+?)</p>" # regular expression 
pattern = re.compile(string) # compiling 
text = f.read() 
search = re.findall(pattern,text) # searching 
print search 
+0

這不是我要找的。 findall找到所有出現的1個正則表達式。我想要所有的subregexps,它們是根據某個更大的正則表達式放置的(所以在上面的情況下 - []。 – robert

+0

@robert)你可以逐行讀取數據,並使其成爲一個循環你的東西 – babygame0ver