2016-04-26 65 views
1
#!/usr/bin/env python 

import sys 
import re 

# regular expressions 

pattern = re.compile("[a-zA-Z]*", 
       re.MULTILINE | re.DOTALL | re.IGNORECASE) 

# Read pairs as lines of input from STDIN 
for line in sys.stdin: 

    # loop through every word that matches the pattern 
    for word in pattern.findall(line): 
     while i < 1: 
      if len(converted_word) != WINDOW: 
       # print "word =", word 
       if a_to_f_pattern.match(word[i]): 
        ..... 

      else: 
       ..... 
     i = 0 

這裏這條線字符串索引超出範圍 - 指數誤差

if a_to_f_pattern.match(word[i]):

讓我在標題中的錯誤,我想不通爲什麼

以前,我有while i < len(word)和它的工作但現在因爲我只想檢查每個單詞的第一個字母,它不起作用。

任何線索?

+0

您的模式是否匹配空字符串? – Bahrom

+0

啊不,它從文本文件中讀取單詞。 –

+0

將'print word'添加到您的循環中。 – Bahrom

回答

1

正則表達式[a-zA-Z]*將匹配空字符串,因爲*表示「零或多個」。請使用[a-zA-Z]+來確保您的單詞至少有一個字母長。

此外,由於您使用的是re.IGNORECASE,因此不需要在模式中同時使用大寫字母和小寫字母。如果模式不包含^$,則不需要re.MULTILINE選項;如果模式中沒有.,則不需要re.DOTALL。所以它應該是:

pattern = re.compile("[a-z]+", re.IGNORECASE) 
+0

嗯只是試過這個,但它給我重複的話? –

+0

如果輸入行有重複的單詞,則正則表達式將返回它們。 – Barmar

+0

歡呼聲謝謝你! –