2017-11-25 11 views
1

私はPythonを使用してJavaソースコードを解析しています。私はソースからコメントテキストを抽出する必要があります。 私は以下を試みました。 [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']Javaのコメントを抽出するPythonの正規表現

2を取るブランク:

cmts = re.findall(r'/\*\*(.|[\r\n])*?\*/', lines)

戻り値:

1テイク

cmts = re.findall(r'(/\*\*(.|[\r\n])*?\*/)', lines)

戻り値(正規表現の周りにグループ化ブラケットを追加した)

1行コメント(例のみ):

('/**\n\n * initialise the tag with the colors and the tag name\n\n */', ' ')

複数行コメント(例のみ):

('/**\n\n * Get the color related to a specified tag\n\n * @param tag the tag that we want to get the colour for\n\n * @return color of the tag in String\n\n */', ' ')

私だけinitialise the tag with the colors and the tag name またはGet the color related to a specified tag, @param tag the tag that we want to get the colour for, @return color of the tag in String に興味とすることはできませんよその周りに私の頭をしてください。私にいくつかの指摘をしてください!コメント(/***/間のすべて)を抽出するには

答えて

1

、あなたが使用することができます。

re.findall(r'\*\*(.*?)\*\/', text, re.S) 

(ドットも改行と一致した場合に、使用されているre.S/re.DOTALL場合キャプチャグループを簡素化することができるかに注意してください)。

その後、各試合のためにあなたは、複数の空白/ *を除去し、交換することができ\n,と:

>>> list(comments('/**\n\n  * Get the color related to a specified tag\n\n  * @param tag the tag that we want to get the colour for\n\n  * @return color of the tag in String\n\n  */')) 
['Get the color related to a specified tag, @param tag the tag that we want to get the colour for, @return color of the tag in String'] 
:たとえば

def comments(text): 
    for comment in re.findall(r'\*\*(.*?)\*\/', text, re.S): 
     yield re.sub('\n+', ',', re.sub(r'[ *]+', ' ', comment).strip())