2016-12-02 6 views
3

いくつかの記事から負傷した人々に関する情報を抽出しようとしています。問題は、ジャーナリス言語で情報を伝えるさまざまな方法があることです。数字または単語で書くことができるからです。例えば正規表現を単語として書かれた数字のリストと組み合わせた

:1-10から行く回数番号のほとんどは言葉ではなく数字で書かれているように私は気づいた

`Security forces had *wounded two* gunmen inside the museum but that two or three accomplices might still be at large.` 

`The suicide bomber has wounded *four men* last night.` 

`*Dozens* were wounded in a terrorist attack.` 

。そして、私はどのような複雑なコードでも起こらずにそれらを抽出する方法を疑問に思っていました。

リストを使用しますか?どのようにそれが含まれるだろうか?

これは私が数字で負傷した人の数を抽出するため、これまでに使用されるパターンです:

two 
four 
Dozens 

\w+\s(?=were)?=

text_open = open("News") 
text_read = text_open.read() 
pattern= ("wounded (\d+)|(\d+) were wounded|(\d+) injured|(\d+) people were wounded|wounding (\d+)|wounding at least (\d+)") 
result = re.findall(pattern,text_read) 
print(result) 

答えて

1

この

import re 

regex = r"(\w)+\s(?=were)|(?<=wounded|injured)\s[\w]{3,}" 

test_str = ("`Security forces had wounded two gunmen inside the museum but that two or three accomplices might still be at large.`\n\n" 
    "`The suicide bomber has wounded four men last night.`\n\n" 
    "`Dozens were wounded in a terrorist attack.") 

matches = re.finditer(regex, test_str) 

for match in matches:  
    print (match.group().strip()) 

出力を試してみてくださいwereを先に見て、 \w

|または

(?<=wounded|injured)\s\w{3,}?<=の背後に見て、キャプチャワード負傷または負傷者が単語の前に発生した場合や{3,}単語の長さを意味は、単にすなわちin単語をキャプチャ避けるために、3以上であり、すべての数値ワードが分を持っています長さ3ですので、それを使用すると問題ありません。

関連する問題