【问题标题】:extracting multi-line from file by using regular expression on python在python上使用正则表达式从文件中提取多行
【发布时间】:2018-01-13 19:43:38
【问题描述】:

我想提取 {"befor " 17}, {"androidID 和 "{"After ", 16}, {"37abc5afce16b6www03", 17}" 在我的文件中使用 python 上的正则表达式

我的文字如下:

      [pid  3342] 12:13:44 pread64(6,"res/color/hint_foreground_material_light.xml", 44, 377177) = 44
      [pid  3342] 12:13:44 mmap(NULL, 597, PROT_READ, MAP_SHARED, 6, 0x5c000) = 0x7f7754c000
      [pid  3342] 12:13:44 munmap(0x7f7754c000, 597) = 0
      [pid  3342] 12:13:44 *writev(4, [{" hZZ  v;", 11}, {" ", 1}, {"befor " 17}, {"androidID ", 10}], 4) = 39*
      [pid  3342] 12:13:44 getuid()           = 11811
      [pid  3342] 12:13:44 getuid()           = 11811
      [pid  3342] 12:13:44 ioctl(11, 0xc0306201, 0x7fc52d8fb0) = 0
      [pid  3342] 12:13:44 ioctl(11, 0xc0306201, 0x7fc52d9180) = 0
      [pid  3342] 12:13:44 writev(3, [{"l ", 7}, {"hZZ ;", 11}, {" ", 1}, {"After ", 16}, {"37abc5afce16b6www03", 17}], 5)= 52
      [pid  3342] 12:13:44 getuid()           = 11811
       ...

有什么建议吗?

【问题讨论】:

  • 一定要用正则表达式吗?如果不是,您可以遍历文件并开始保存,如果该行包含 ' {"befor " 17}, {"androidID' 并在该行包含结束值时停止。
  • 你的方法的问题是,有时我有“之前”和 androidID 不是单一的
  • @kaloon,发布最终预期结果

标签: python regex data-extraction


【解决方案1】:

试试这个r"(?m)^.*?\{\"befor[ ]\"[ ]17\},[ ]\{\"androidID.*[\S\s]+?^.*?\{\"After[ ]\",[ ]16\},[ ]\{\"37abc5afce16b6www03\",[ ]17\}.*"

https://regex101.com/r/fyPZpq/1

 (?m)
 ^ .*? \{"befor [ ] " [ ] 17\}, [ ] \{"androidID .* 
 [\S\s]+? 
 ^ .*? \{"After [ ] ", [ ] 16\}, [ ] \{"37abc5afce16b6www03", [ ] 17\} .* 

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    相关资源
    最近更新 更多