Python(2.7)で詳細な正規表現を使用しようとしています。重要なのは、いつか戻って表現をより明確に理解できるようにすることです。私は新しいので、最初にコンパクトな式を作成して、自分が望むものを確実に取得できるようにしました。
コンパクトな表現は次のとおりです。
test_verbose_item_pattern = re.compile('\n{1}\b?I[tT][eE][mM]\s+\d{1,2}\.?\(?[a-e]?\)?.*[^0-9]\n{1}')
期待どおりに動作します
これが詳細表現です
verbose_item_pattern = re.compile("""
\n{1} #begin with a new line allow only one new line character
\b? #allow for a word boundary the ? allows 0 or 1 word boundaries \nITEM or \n ITEM
I # the first word on the line must begin with a capital I
[tT][eE][mM] #then we need one character from each of the three sets this allows for unknown case
\s+ # one or more white spaces this does allow for another \n not sure if I should change it
\d{1,2} # require one or two digits
\.? # there could be 0 or 1 periods after the digits 1. or 1
\(? # there might be 0 or 1 instance of an open paren
[a-e]? # there could be 0 or 1 instance of a letter in the range a-e
\)? # there could be 0 or 1 instance of a closing paren
.* #any number of unknown characters so we can have words and punctuation
[^0-9] # by its placement I am hoping that I am stating that I do not want to allow strings that end with a number and then \n
\n{1} #I want to cut it off at the next newline character
""",re.VERBOSE)
問題は、冗長パターンを実行すると例外が発生することです
Traceback (most recent call last):
File "C:/Users/Dropbox/directEDGAR-Code-Examples/NewItemIdentifier.py", line 17, in <module>
""",re.VERBOSE)
File "C:\Python27\lib\re.py", line 190, in compile
return _compile(pattern, flags)
File "C:\Python27\lib\re.py", line 242, in _compile
raise error, v # invalid expression
error: nothing to repeat
これはばかげたことになるのではないかと思いますが、理解できません。私は冗長な表現を取り、それを1行ずつ圧縮して、コンパクトバージョンが冗長と同じであることを確認しました。
エラーメッセージには、繰り返すものは何もないと記載されていますか?