0

Python の正規表現について助けが必要です。次のような文字列があります。

17:25:31;http://example1.com/viewtopic.php?f=8&t=189;example1.com;127.0.0.1 2013-10-19
17:22:32;http://example2.com;example2.com;127.0.0.1 2013-10-19 
20:18:28;http://example3.com/threads/example-text-in-url.27304/;example3.com;127.0.0.1 2013-10-19

このリストを取得するにはどうすればよいですか?

['http://example1.com/viewtopic.php?f=8&t=189', 'http://example2.com', 'http://example3.com/threads/example-text-in-url.27304/']
4

3 に答える 3

1

それがあなたが求めたものなので、正規表現ソリューションを提供します。http://基本的に、との間のテキストをキャプチャするだけです;。以下はデモンストレーションです。

from re import findall

mystr = """
17:25:31;http://example1.com/viewtopic.php?f=8&t=189;example1.com;127.0.0.1 2013-10-19
17:22:32;http://example2.com;example2.com;127.0.0.1 2013-10-19 
20:18:28;http://example3.com/threads/example-text-in-url.27304/;example3.com;127.0.0.1  2013-10-19
"""

print findall("(http://.+?);", mystr)

出力:

['http://example1.com/viewtopic.php?f=8&t=189', 'http://example2.com', 'http://example3.com/threads/example-text-in-url.27304/']
于 2013-10-19T18:41:00.317 に答える
1

これを試してみてください。多分それはあなたのニーズに合っています:)

正規表現

/^(.*;)/gm

17:25:31;http://example1.com/viewtopic.php?f=8&t=189;example1.com;127.0.0.1 2013-10-19
17:22:32;http://example2.com;example2.com;127.0.0.1 2013-10-19 
20:18:28;http://example3.com/threads/example-text-in-url.27304/;example3.com;127.0.0.1 2013-10-19

マッチ

1.  [0-66]    `17:25:31;http://example1.com/viewtopic.php?f=8&t=189;example1.com;`
2.  [87-129]  `17:22:32;http://example2.com;example2.com;`
3.  [151-228] `20:18:28;http://example3.com/threads/example-text-in-url.27304/;example3.com
于 2013-10-19T18:34:43.090 に答える