2017-12-24 3 views
0

저는 파이썬에서 단어의 양을 넣은 다음 단어 목록에서 해당 양의 숯을 가진 단어를 검색하는 무언가를 만들려고합니다.파이썬 검색에서 정규식

내 코드 :

import sys 

import re 

def search(pattern): 

    print("Searching...\n\n") 

    for i, line in enumerate(open(sys.argv[1])): 

     for match in re.finditer(pattern, line): 

      print(match.groups()) 


    print("\n\nFinished.") 


while True: 

    word = "" 

    put = int(input("Amount of Letters in Word: ")) 

    if put > 25 or put < 3: 

     print("Invalid amount of letters.") 

    else: 

     for n in range(0, put): 

      word = word + "." 

     word = "^" + word + "$" 

     pattern = re.compile(word) 

     search(pattern) 

나는 당신이 넣어 문자의 양을 가진 모든 단어를 보여주고 싶어요. 단어의

https://i.imgur.com/Kgusvyh.png

목록 :

word 
1234 
okay 
0000 
asdfg 
asdfh 
asdgj 

왜 (보여 않음)?

+1

매번 match.groups를 인쇄하고 있습니다. 아마도 빈 터플을 인쇄하고있을 것입니다. [this] (https://stackoverflow.com/questions/7312020/why-wont-re-groups-give-me-anything-for-my-one-correctly-matched-group) 이유가 될 수도 있습니다 – 0TTT0

+0

Nvm, fixed by match.groups()를 match.group()으로 대체합니다. – Boobah

답변

0

match.groups()를 match.group()으로 대체하여 수정되었습니다.