免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3296 | 回复: 1
打印 上一主题 下一主题

求助。python正则表达式匹配段文字 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-10-31 18:58 |只看该作者 |倒序浏览
  1. [2011/10/31 01:25:27.990]cookieuid=============
  2. [2011/10/31 01:25:27.998]==/UserUid_10000/app_0/page=mobile/p1=mobile/p2=1/time=158
  3. [2011/10/31 01:25:27.998]==/userBehavior_10000/page=mobile/act1=1/act2=
  4. [2011/10/31 01:25:37.208]=========load records:0
  5. [2011/10/31 01:25:50.031]cookieuid=============
  6. [2011/10/31 01:25:50.032]<<<< update:1
  7. [2011/10/31 01:25:50.061]java.lang.NullPointerException
  8. [2011/10/31 01:25:50.061]       at java.util.Collections.sort(Collections.java:158)
  9. [2011/10/31 01:25:50.061]       at sssssgame.pic.bless.BlessIndexHivImp.showAllFriend(BlessIndexHivImp.java:300)
  10. [2011/10/31 01:25:50.061]       at sssssgame.pic.bless.BlessIndexHivImp.renderPage(BlessIndexHivImp.java:67)
  11. [2011/10/31 01:25:50.062]       at sssssgame.page.bless.BlessIndexPageImp.comReplace(BlessIndexPageImp.java:32)
  12. [2011/10/31 01:25:50.062]       at sssssgame.page.bless.BlessIndexPageImp.dealWith(BlessIndexPageImp.java:26)
  13. [2011/10/31 01:25:50.062]       at sssssgame.page.AbstractServlet.service(AbstractServlet.java:206)
  14. [2011/10/31 01:25:50.062]       at sssssgame.page.ApplicationServlet.service(ApplicationServlet.java:23)
  15. [2011/10/31 01:25:50.062]       at javax.servlet.http.HttpServlet.service(HttpServlet.java:91)
  16. [2011/10/31 01:25:50.062]       at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:103)
  17. [2011/10/31 01:25:50.062]       at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:201)
  18. [2011/10/31 01:25:50.062]       at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:189)
  19. [2011/10/31 01:25:50.062]       at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:266)
  20. [2011/10/31 01:25:50.062]       at com.caucho.server.hmux.HmuxRequest.handleRequest(HmuxRequest.java:463)
  21. [2011/10/31 01:25:50.062]       at com.caucho.server.port.TcpConnection.handleRequests(TcpConnection.java:577)
  22. [2011/10/31 01:25:50.062]       at com.caucho.server.port.TcpConnection$AcceptTask.doAccept(TcpConnection.java:1211)
  23. [2011/10/31 01:25:50.062]       at com.caucho.server.port.TcpConnection$KeepaliveTask.run(TcpConnection.java:1282)
  24. [2011/10/31 01:25:50.062]       at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:759)
  25. [2011/10/31 01:25:50.062]       at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:681)
  26. [2011/10/31 01:25:50.062]       at java.lang.Thread.run(Thread.java:619)
  27. [2011/10/31 01:25:50.135]==/UserUid_10000/app_0/page=BlessIndex/p1=/p2=/time=110
  28. [2011/10/31 01:25:50.135]==/userBehavior_10000/page=BlessIndex/act1=2/act2=
  29. [2011/10/31 01:26:07.568]cookieuid=============
  30. [2011/10/31 01:26:07.575]3050
  31. [2011/10/31 01:26:07.589]3050
  32. [2011/06/13 08:13:38.277]uid=============998
  33. [2011/06/13 08:13:38.278]==/UserUid_998/app_0/page=AlbumIndex/p1=postzp/p2=/time=0
  34. [2011/06/13 08:13:38.429]==/UserUid_998/app_0/page=AlbumIndex/p1=/p2=/time=158
  35. [2011/06/13 08:13:38.430]==/userBehavior_998/page=AlbumIndex/act1=6/act2=
  36. [2011/06/13 08:13:39.574]cookieuid=============
  37. [2011/06/13 08:13:39.575]==/UserUid_998/app_0/page=AlbumIndex/p1=postzp/p2=/time=0
  38. [2011/06/13 08:13:39.577]org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
  39. [2011/06/13 08:13:39.577]   at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:885)
  40. [2011/06/13 08:13:39.577]   at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
  41. [2011/06/13 08:13:39.577]   at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:349)
  42. [2011/06/13 08:13:39.577]   at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
  43. [2011/06/13 08:13:39.577]   at sssssgame.pic.album.AlbumHivImp.doUploadPhoto(AlbumHivImp.java:213)
  44. [2011/06/13 08:13:39.577]   at sssssgame.pic.album.AlbumIndexHivImp.noviceUploadPhoto(AlbumIndexHivImp.java:1379)
  45. [2011/06/13 08:13:39.577]   at sssssgame.pic.album.AlbumIndexHivImp.renderPage(AlbumIndexHivImp.java:82)
  46. [2011/06/13 08:13:39.578]   at sssssgame.page.album.AlbumIndexPageImp.comReplace(AlbumIndexPageImp.java:37)
  47. [2011/06/13 08:13:39.578]   at sssssgame.page.album.AlbumIndexPageImp.dealWith(AlbumIndexPageImp.java:31)
  48. [2011/06/13 08:13:39.578]   at sssssgame.page.AbstractServlet.service(AbstractServlet.java:201)
  49. [2011/06/13 08:13:39.578]   at sssssgame.page.ApplicationServlet.service(ApplicationServlet.java:23)
  50. [2011/06/13 08:13:39.578]   at javax.servlet.http.HttpServlet.service(HttpServlet.java:91)
  51. [2011/06/13 08:13:39.578]   at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:103)
  52. [2011/06/13 08:13:39.578]   at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:201)
  53. [2011/06/13 08:13:39.578]   at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:189)
  54. [2011/06/13 08:13:39.578]   at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:266)
  55. [2011/06/13 08:13:39.578]   at com.caucho.server.hmux.HmuxRequest.handleRequest(HmuxRequest.java:463)
  56. [2011/06/13 08:13:39.578]   at com.caucho.server.port.TcpConnection.handleRequests(TcpConnection.java:577)
  57. [2011/06/13 08:13:39.578]   at com.caucho.server.port.TcpConnection$KeepaliveTask.run(TcpConnection.java:1266)
  58. [2011/06/13 08:13:39.578]   at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:759)
  59. [2011/06/13 08:13:39.578]   at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:681)
  60. [2011/06/13 08:13:39.578]   at java.lang.Thread.run(Thread.java:619)
  61. [2011/06/13 08:13:39.607]==/UserUid_998/app_0/page=AlbumIndex/p1=/p2=/time=38
  62. [2011/06/13 08:13:39.607]==/userBehavior_998/page=AlbumIndex/act1=6/act2=
  63. [2011/06/13 08:13:46.157]cookieuid=============
  64. [2011/06/13 08:13:46.192]==/UserUid_998/app_0/page=UserIndex/p1=/p2=/time=40
  65. [2011/06/13 08:13:46.192]==/userBehavior_998/page=UserIndex/act1=2/act2=
  66. [2011/06/13 08:13:46.768]==/UserUid_0/app_0/page=Login/p1=/p2=/time=4
复制代码
我现在要提取,java.lang.NullPointerException 到[2011/10/31 01:25:50.062]到 at java.lang.Thread.run(Thread.java:619) 这段文字存到数组里面
还是有org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null 到[2011/06/13 08:13:39.578]   at java.lang.Thread.run(Thread.java:619)
用python的正则表达式来做,请问要怎么处理

我用如下表达式来做
  1. rawstr = r"""(java\.lang\.(\S.+)\n|org.apache.(\S.+))\S.+?java\.lang\.Thread\.run\(\S.+\)\n"""
  2. compile_obj = re.compile(rawstr,  re.S|re.M)
  3. match_obj = compile_obj.search(txt)
  4. if match_obj:
  5.     print match_obj.group()
复制代码
但是变成把
  1. [2011/10/31 01:25:50.062]       at java.lang.Thread.run(Thread.java:619)
  2. [2011/10/31 01:25:50.135]==/UserUid_10000/app_0/page=BlessIndex/p1=/p2=/time=110
  3. [2011/10/31 01:25:50.135]==/userBehavior_10000/page=BlessIndex/act1=2/act2=
  4. [2011/10/31 01:26:07.568]cookieuid=============
  5. [2011/10/31 01:26:07.575]3050
  6. [2011/10/31 01:26:07.589]3050
  7. [2011/06/13 08:13:38.277]uid=============998
  8. [2011/06/13 08:13:38.278]==/UserUid_998/app_0/page=AlbumIndex/p1=postzp/p2=/time=0
  9. [2011/06/13 08:13:38.429]==/UserUid_998/app_0/page=AlbumIndex/p1=/p2=/time=158
  10. [2011/06/13 08:13:38.430]==/userBehavior_998/page=AlbumIndex/act1=6/act2=
  11. [2011/06/13 08:13:39.574]cookieuid=============
  12. [2011/06/13 08:13:39.575]==/UserUid_998/app_0/page=AlbumIndex/p1=postzp/p2=/time=0
  13. [2011/06/13 08:13:39.577]org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
复制代码
也匹配到了,纠结


理想结果是
  1. ["java.lang.NullPointerException
  2. [2011/10/31 01:25:50.061]       at java.util.Collections.sort(Collections.java:158)
  3. [2011/10/31 01:25:50.061]       at sssssgame.pic.bless.BlessIndexHivImp.showAllFriend(BlessIndexHivImp.java:300)
  4. [2011/10/31 01:25:50.061]       at sssssgame.pic.bless.BlessIndexHivImp.renderPage(BlessIndexHivImp.java:67)
  5. [2011/10/31 01:25:50.062]       at sssssgame.page.bless.BlessIndexPageImp.comReplace(BlessIndexPageImp.java:32)
  6. [2011/10/31 01:25:50.062]       at sssssgame.page.bless.BlessIndexPageImp.dealWith(BlessIndexPageImp.java:26)
  7. [2011/10/31 01:25:50.062]       at sssssgame.page.AbstractServlet.service(AbstractServlet.java:206)
  8. [2011/10/31 01:25:50.062]       at sssssgame.page.ApplicationServlet.service(ApplicationServlet.java:23)
  9. [2011/10/31 01:25:50.062]       at javax.servlet.http.HttpServlet.service(HttpServlet.java:91)
  10. [2011/10/31 01:25:50.062]       at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:103)
  11. [2011/10/31 01:25:50.062]       at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:201)
  12. [2011/10/31 01:25:50.062]       at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:189)
  13. [2011/10/31 01:25:50.062]       at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:266)
  14. [2011/10/31 01:25:50.062]       at com.caucho.server.hmux.HmuxRequest.handleRequest(HmuxRequest.java:463)
  15. [2011/10/31 01:25:50.062]       at com.caucho.server.port.TcpConnection.handleRequests(TcpConnection.java:577)
  16. [2011/10/31 01:25:50.062]       at com.caucho.server.port.TcpConnection$AcceptTask.doAccept(TcpConnection.java:1211)
  17. [2011/10/31 01:25:50.062]       at com.caucho.server.port.TcpConnection$KeepaliveTask.run(TcpConnection.java:1282)
  18. [2011/10/31 01:25:50.062]       at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:759)
  19. [2011/10/31 01:25:50.062]       at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:681)
  20. [2011/10/31 01:25:50.062]       at java.lang.Thread.run(Thread.java:619)",
  21. "org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null[2011/06/13 08:13:39.577]   at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:885)
  22. [2011/06/13 08:13:39.577]   at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
  23. [2011/06/13 08:13:39.577]   at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:349)
  24. [2011/06/13 08:13:39.577]   at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
  25. [2011/06/13 08:13:39.577]   at sssssgame.pic.album.AlbumHivImp.doUploadPhoto(AlbumHivImp.java:213)
  26. [2011/06/13 08:13:39.577]   at sssssgame.pic.album.AlbumIndexHivImp.noviceUploadPhoto(AlbumIndexHivImp.java:1379)
  27. [2011/06/13 08:13:39.577]   at sssssgame.pic.album.AlbumIndexHivImp.renderPage(AlbumIndexHivImp.java:82)
  28. [2011/06/13 08:13:39.578]   at sssssgame.page.album.AlbumIndexPageImp.comReplace(AlbumIndexPageImp.java:37)
  29. [2011/06/13 08:13:39.578]   at sssssgame.page.album.AlbumIndexPageImp.dealWith(AlbumIndexPageImp.java:31)
  30. [2011/06/13 08:13:39.578]   at sssssgame.page.AbstractServlet.service(AbstractServlet.java:201)
  31. [2011/06/13 08:13:39.578]   at sssssgame.page.ApplicationServlet.service(ApplicationServlet.java:23)
  32. [2011/06/13 08:13:39.578]   at javax.servlet.http.HttpServlet.service(HttpServlet.java:91)
  33. [2011/06/13 08:13:39.578]   at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:103)
  34. [2011/06/13 08:13:39.578]   at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:201)
  35. [2011/06/13 08:13:39.578]   at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:189)
  36. [2011/06/13 08:13:39.578]   at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:266)
  37. [2011/06/13 08:13:39.578]   at com.caucho.server.hmux.HmuxRequest.handleRequest(HmuxRequest.java:463)
  38. [2011/06/13 08:13:39.578]   at com.caucho.server.port.TcpConnection.handleRequests(TcpConnection.java:577)
  39. [2011/06/13 08:13:39.578]   at com.caucho.server.port.TcpConnection$KeepaliveTask.run(TcpConnection.java:1266)
  40. [2011/06/13 08:13:39.578]   at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:759)
  41. [2011/06/13 08:13:39.578]   at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:681)
  42. [2011/06/13 08:13:39.578]   at java.lang.Thread.run(Thread.java:619)"]
复制代码

论坛徽章:
1
丑牛
日期:2013-09-29 19:04:50
2 [报告]
发表于 2011-11-02 15:32 |只看该作者
  1. #! /usr/bin/python
  2. #encoding=utf-8

  3. import re
  4. srcfile="in.txt"

  5. patterm = re.compile("^\[.*?\]\s+?at")
  6. patterm2 =re.compile("Exception")
  7. try:
  8.         f=open(srcfile)
  9.         fout = open("out.txt","w")
  10. except:
  11.         raise IOError('..')

  12. lines = [line for line in f.readlines()]

  13. for line in lines:
  14.         content = patterm.search(line)
  15.         if content is not None:
  16.                 fout.write(line)
  17.         content = patterm2.search(line)
  18.         if content is not None:
  19.                 fout.write(line)

  20. f.close()
  21. fout.close()
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP