为什么我的正则表达式扫描不起作用?

时间:2013-05-14 15:55:53

标签: ruby regex

我有一个脚本可以扫描文本文件并将其放入CSV文件中。它会抓取debtor信息,然后将creditor信息放在其后面。

问题是,它会获得每个debtor,但会为每个creditor添加相同的debtor信息,而不是debtor下面的新信息:

FasterCSV.open('data.csv', 'a') do |csv|

  debtor_info = results.scan(/^(\d{2}\-\d{5})(\s+)(.*)(\s+)(Total:)(\s+)(\$(\d+\,?   \.?)+)/)

   debtor_info.each do |line|
   case_number = line.at(0)
   debtor = line.at(2).strip
   total_amount = line.at(6)
   csv << [case_number, debtor, total_amount]

    creditor_info = results.scan(/((\d{1,2})\/(\d{1,2})\/(\d{1,4}))\s+(\$(\d+\,?\.?)+)\s+(\d{1,5}BK)\s+(.*)/)

 creditor_info.each do |info|
       date = info.at(0)
       amount = info.at(4)
       fund_number = info.at(6)
       creditor = info.at(7)
       empty = " "
       csv << [empty, date, amount, fund_number, creditor]  

   end
 end
end

这是一些示例输入:

00-000##     Company Inc                            Total: $3,134.55

  2/25/2003       $416.02    0000BK       A Comp Inc
  2/25/2003       $105.60    0000BK       California Imprinted Apparel

  2/25/2003        $58.41    0000BK       John Doe

  2/25/2003        $33.41    0000BK       E Doe & Assoc
  2/25/2003        $78.28    0000BK       Candle Candles

  2/25/2003        $44.74    0000BK       Personnel Svcs
  2/25/2003        $28.34    0000BK       Jane Doe

  2/25/2003        $32.77    0000BK       Water Co

  2/25/2003       $141.21    0000BK       XYX
  2/25/2003       $250.96    0000BK       PDQ INC

  2/25/2003       $146.17    0000BK       RS FM
  2/25/2003       $722.91    0000BK       A Corp

  2/25/2003       $841.14    0000BK       BAC Corp
  2/25/2003       $202.57    0000BK       ABC Communications

  2/25/2003        $32.02    0000BK       YXY SA Corp

00-00128     May June                                           Total: $29.60
  6/26/2002        $29.60    0000BK       May June

00-00653     Joe Doey                                             Total: $347.10

  7/10/2002        $59.62    0000BK       Financial Corp
  7/10/2002       $287.48    0000BK       ABC Corp

00-00657     Thomas P Public                                        Total: $1,096.75
   7/2/2003     $1,096.75    0000BK       Contract Svc

00-00735     Jean Jane                                            Total: $29.89

  6/18/2003        $29.89    0000BK       Jean Jane

1 个答案:

答案 0 :(得分:1)

使用给定的结构,您需要将 债权人或债务人行扫描到单个列表中,然后通过它,设置&#34;当前债务人&#34;无论何时遇到一个。

假设results实际上只是输入文件的一个啜食(你不能说,但看起来很可能):

combined_info = results.scan(/^(\d{2}\-\d{5})(\s+)(.*)(\s+)(Total:)(\s+)(\$(\d+\,?\d+\.?)+)|((\d{1,2})\/(\d{1,2})\/(\d{1,4}))\s+(\$(\d+\,?\.?)+)\s+(\d{1,5}BK)\s+(.*)/)
case_number = "unknown"
debtor = "unknown"
total_amount = "unknown"

combined_info.each do |line|
  # If it's a debtor, set variables, no output
  if line.at(0)
    case_number = line.at(0)
    debtor = line.at(2).strip
    total_amount = line.at(6)
    next
  end

  # A creditor, so collect data and output, note our capture indices have moved . . .
  date = line.at(8)
  amount = line.at(12)
  fund_number = line.at(14)
  creditor = line.at(15)
  empty = " "
  csv << [case_number, debtor, total_amount, empty, date, amount, fund_number, creditor]
end

你的正则表达式也可以做一些工作(例如删除不必要的捕获),但这应该有希望让你开始。

可能还有其他方法以更干净的方式适合此数据输入 - 例如在输入期间逐行识别而不是使用.scan - 但是这个答案旨在建立在您现有的方法之上。