Posted By

webonomic on 10/26/10


Tagged

regex data strip text regular Expression delimiters


Versions (?)

Strip characters from between two delimiters


 / Published in: Ruby
 

This regular expression used in Ruby will extract the characters between single or multi-character delimiters. It helps when you have strange delimiters such as \":delimeter1: text I want to grab goes here :delimiter2:\"

  1. #Example row of data: 11/10/2010:Location: Home :Gender: M :Comment: I just wanted to say I had a great time at the party! :Email: [email protected]
  2.  
  3. file = File.new("data.dlm", "r")
  4. File.open('output.txt', 'w') do |f1| while (line = file.gets)
  5. sub_string = line.scan(/:Comment:([^<>]*):Email:/imu).flatten
  6. f1.puts sub_string
  7. end
  8. end
  9. file.close

Report this snippet  

You need to login to post a comment.