正则表达式过滤表

时间:2013-02-26 07:02:23

标签: php ruby regex ruby-on-rails-3 codeigniter

好吧,我有一个表可以通过一些开源软件输出,但它不会以实际的表格格式输出,例如

<table> 
  <thead>
     <td>Heading</td>
  <thead>
  <tbody>
    <tr>
       <td>Content</td>
    </tr>
  <tbody>
</table
相反,开发软件的人决定输出表格是个好主意

+------------+-------------+-------+-------------+------------+---------------+----------+
| HEADING 1  | HEADING 2   | ETC   | ANOTHER     | HEADING3   | HEADING4     | SML |
+------------+-------------+-------+-------------+------------+---------------+----------+
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
+------------+-------------+-------+-------------+------------+--------------+----------+
| TOTALS        AGENTS:21  |  total|        total|       total|         total| total|
+------------+-------------+-------+-------------+------------+--------------+----------+

所以我不能建立一个网络刮板来获取数据,或者我不能保证,如果我可以构建一个刮刀来刮除它,因为它全部包裹在一个<pre> </pre>标签内。所以相反,我一直试图使用ruby和Regex尝试完成工作到目前为止我已经设法获得所有领先的|,并且我已经设法获得标题+-------+-----但只有那么远,因为它似乎我必须重复模式,它不想重复自己好了但是现在谈论足够这是我到目前为止使用的代码

text.lines.to_a.each do |line|
   line.sub(/^\| |^\+*-*\+*\-*/) do |match|
    puts "Regexp Match: " << match
end
STDIN.getc
puts "New Line "<< line
end

例如,第一行的输出只是+-----------------+---------- 它采用CSV格式,因此请使用Gsub将剩余的|替换为,

我可以使用PHP或Ruby,所以任何答案都非常受欢迎

4 个答案:

答案 0 :(得分:3)

这可能不是那么干净但它适用于这个例子:) 红宝石:

@text = <<END
+------------+-------------+-------+-------------+------------+---------------+----------+
| HEADING 1  | HEADING 2   | ETC   | ANOTHER     | HEADING3   | HEADING4     | SML |
+------------+-------------+-------+-------------+------------+---------------+----------+
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
+------------+-------------+-------+-------------+------------+--------------+----------+
| TOTALS        AGENTS:21  |  total|        total|       total|         total| total|
+------------+-------------+-------+-------------+------------+--------------+----------+
END
s = @text.scan(/^[|]\W(.*)[|]$/)
puts s
arr = []
arr2 = []
s.each do |o|
  a = o.to_s.split('|')
    a.each do |oo|
      arr2 << oo.to_s.gsub('["','').gsub('"]','').gsub(/\s+/, "")
    end
    arr << arr2
  arr2 = []
end
arr.each do |i|
  puts i
end

答案 1 :(得分:2)

退房:

$table = '+------------+-------------+-------+-------------+------------+---------------+----------+
| HEADING 1  | HEADING 2   | ETC   | ANOTHER     | HEADING3   | HEADING4     | SML |
+------------+-------------+-------+-------------+------------+---------------+----------+
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
+------------+-------------+-------+-------------+------------+--------------+----------+
| TOTALS        AGENTS:21  |  total|        total|       total|         total| total|
+------------+-------------+-------+-------------+------------+--------------+----------+';

$lines = preg_split('/\r\n|\r|\n/', $table);
$array = array();
foreach($lines as $line){
  if(!preg_match('/\+-+\+/', $line)){
    $array[] = preg_split('/\s*\|\s*/', trim($line, '| '));
  }
}

print_r($array);

输出:

Array
(
    [0] => Array
        (
            [0] => HEADING 1
            [1] => HEADING 2
            [2] => ETC
            [3] => ANOTHER
            [4] => HEADING3
            [5] => HEADING4
            [6] => SML
        )

    [1] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [2] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [3] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [4] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [5] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [6] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [7] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [8] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [9] => Array
        (
            [0] => TOTALS        AGENTS:21
            [1] => total
            [2] => total
            [3] => total
            [4] => total
            [5] => total
        )

)

希望这有用:)

答案 2 :(得分:2)

这是ruby的完整解决方案。但是,您需要手动将|添加到最后一行。

require 'builder'

table = '+------------+-------------+-------+-------------+------------+---------------+----------+
| HEADING 1  | HEADING 2   | ETC   | ANOTHER     | HEADING3   | HEADING4     | SML |
+------------+-------------+-------+-------------+------------+---------------+----------+
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
+------------+-------------+-------+-------------+------------+--------------+----------+
| TOTALS        AGENTS:21  |  total|        total|       total|         total| total|
+------------+-------------+-------+-------------+------------+--------------+----------+';

def parse_table(table)
  rows = []
  table.each_line do |line|
    next if line.match /^\+/
    rows << line.split(/\s*\|\s*/).reject(&:empty?) 
  end
  rows
end

def html_row(xml, columns)
  xml.tr do
    columns.each do |column|
      xml.td column
    end
  end
end

def html_table(rows)
  head_row = rows.first
  body_rows = rows[1..-1]

  xml = Builder::XmlMarkup.new :indent => 2
  xml.table do
    xml.thead do
      html_row xml, head_row
    end
    xml.tbody do
      body_rows.each do |body_row|
        html_row xml, body_row
      end
    end
  end.to_s
end


rows = parse_table(table)
html = html_table(rows)
puts html

输出:

<table>
  <thead>
    <tr>
      <td>HEADING 1</td>
      <td>HEADING 2</td>
      <td>ETC</td>
      <td>ANOTHER</td>
      <td>HEADING3</td>
      <td>HEADING4</td>
      <td>SML</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>TOTALS        AGENTS:21</td>
      <td>total</td>
      <td>total</td>
      <td>total</td>
      <td>total</td>
      <td>total</td>
    </tr>
  </tbody>
</table>

答案 3 :(得分:0)

对于将字段从表格中取出的主要工作,使用带有模式的split来获取每一行:

line.split(/\s*\|\s*/)

这会根据每个|和任何周围的空格将行拆分为一个数组。丢弃数组的第一个和最后一个元素,因为模式也匹配开头和结尾|