Subscribe to The New and Improved Ruby Codes        RSS Feed
-----

Ruby - Scan() and Regex: Converting a paragraph valuable data values

Icon Leave Comment
In ruby, scan is a powerful method that you can use with regex to scan through a string and find all occurrences of a pattern. So if you have a string with data in it, say, maybe you are parsing from emails or some other secondary data source, you would use regex to parse that important information into your ruby app.

Props to: Karel-Lodewijk

ref: http://www.dreaminco...rse-into-a-hash


Here's our sample string, not to confuse you, but we are using HERE-DOCs to put into a string variable a "document" inline within our code.

my_string = <<DOC
#hello {
   used: "no wai!";
   content: "hi there";
}
s
#goodbye {
   content: "fare well";
}
DOC




Not too tricky, eh? Then we can start testing scan on our string.

(To scan through a string and see if there are any occurances of a plain string)
return_array = my_string.scan("#hello")     #=> ["#hello"]



As you can see, it returns an array containing all matches. Pretty boring if you're scanning with plain strings, but if you're using regex, the return array becomes much more interesting.


(scan through a string using regex)
return_array = my_string.scan(/#\S*/)     #=> ["#hello", "#goodbye"]




Interesting, but that pattern doesn't take us very far if we'd like to get the associations between #<id> and Content: <content>. To achieve such a capture, we'll need to make two passes.

Pass 1. Capture <id> and <{...}>.
#=> [ ["hello",  "\n   used: \"no wai!\";\n   content: \"hi there\"; \n"], ... ]




pass 2. Capture <content>
#=> ["hi there"]




And here's the first step.

(Pass 1)
top_levels = my_string.scan /#(\w+?)\s*{(.*?)}/m



(pass 2)
for top_level in top_levels
    first_part = top_level[0]
    second_part = top_level[1].scan /(\w+?):\s*"(.*?)"\s*;/m
    p  "name " + first_part
    p second_part
end



Lovely.

Spoiler

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

October 2014

S M T W T F S
   1234
567891011
12131415161718
19202122232425
26272829 30 31 

Tags

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)

    Categories