tags:

views:

86

answers:

4

How do I go from this:

"01","35004","AL","ACMAR",86.51557,33.584132,6055,0.001499 

to this:

ACMAR, AL
+1  A: 

If it is an array — […].grep(/^[A-Z]+$/), if string — "…".scan(/[A-Z]+/)

tig
A: 

match: \"([A-Za-z].*)\".*\"([A-Za-z].*)\"

replace: \2, \1

This assumes the only fields with letters are those two. A better solution might be to explode on ',' and take the 3rd and 4th string.

J Hall
How would I run this?\"([A-Za-z].*)\".*\"([A-Za-z].*)\".match("01","35004","AL","ACMAR",86.51557,33.584132,6055,0.001499) This is not right.
mer
use the string sub function, something like this: localString.sub(/\"([A-Za-z].*)\".*\"([A-Za-z].*)\"/,"\2, \1");
J Hall
A: 

You could use this

"(.+)","(.+)","(.+)","(.+)",

and then concatenate capture group 3 and 2

Tahbaza
+1  A: 

I'm not quite sure why you'd want to parse a CSV file with a Regexp instead of a CSV parser. It makes your life so much easier:

require 'csv'

CSV.open('/path/to/output.csv', 'wt') do |csv|
  CSV.foreach('/path/to/output.csv') do |_, _, state, city|
    csv << [state, city]
  end
end
Jörg W Mittag