<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: match code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 02:49:10 GMT</pubDate>
    <description>DZone Snippets: match code</description>
    <item>
      <title>Basic Trie data structure in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5616</link>
      <description>&lt;code&gt;&lt;br /&gt;# Inspired by http://www.rubyquiz.com/quiz103.html&lt;br /&gt;# cf. also http://en.wikipedia.org/wiki/Trie&lt;br /&gt;&lt;br /&gt;#!/usr/local/bin/ruby -w&lt;br /&gt;&lt;br /&gt;require 'pp'&lt;br /&gt;&lt;br /&gt;class Hash&lt;br /&gt;&lt;br /&gt;   def deep_merge!(second)&lt;br /&gt;      # cf. http://snippets.dzone.com/posts/show/4146&lt;br /&gt;      merger = proc { |key,v1,v2| Hash === v1 &amp;&amp; Hash === v2 ? v1.merge(v2, &amp;merger) : v2 }&lt;br /&gt;      self.merge!(second, &amp;merger)&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def nested_hash(array)&lt;br /&gt;      node = self&lt;br /&gt;      array.each do |i|&lt;br /&gt;         node[i]=Hash.new if node[i].nil?&lt;br /&gt;         node = node[i]&lt;br /&gt;      end &lt;br /&gt;      self&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def merge_nested_hash!(nested_hash)&lt;br /&gt;      deep_merge!(nested_hash)&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   # code basis taken from: "Find every path and it's value in a Hash" by Florian A&#223;mann,&lt;br /&gt;   # http://snippets.dzone.com/posts/show/3565&lt;br /&gt;&lt;br /&gt;   def each_trie_path&lt;br /&gt;      raise ArgumentError unless block_given?&lt;br /&gt;      self.class.each_trie_path(self) { |path, object| yield(path, object) }&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   protected&lt;br /&gt;   def self.each_trie_path(object, path = [], &amp;block)  &lt;br /&gt;&lt;br /&gt;      if object.is_a?(Hash)&lt;br /&gt;         object.each do |key, value|&lt;br /&gt;&lt;br /&gt;            if key == true &amp;&amp; value == {}&lt;br /&gt;               if path == [:root]  # special case for empty string: [[:root], {true=&gt;{}}]&lt;br /&gt;                  yield(path, {true=&gt;{}})&lt;br /&gt;                  next&lt;br /&gt;               end&lt;br /&gt;               yield(path, object)&lt;br /&gt;            end&lt;br /&gt;&lt;br /&gt;            self.each_trie_path(value, [path , key].flatten, &amp;block)&lt;br /&gt;         end&lt;br /&gt;      else &lt;br /&gt;         yield(path, object)&lt;br /&gt;      end&lt;br /&gt;   end &lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Trie&lt;br /&gt;&lt;br /&gt;   @hash = Hash.new.merge_nested_hash!(Hash.new)&lt;br /&gt;   #@hash = Hash.new.merge_nested_hash!({:root=&gt;{}})&lt;br /&gt;   #@hash = Hash.new.merge_nested_hash!(Hash.new.nested_hash([:root]))&lt;br /&gt;   class &lt;&lt; self; attr_accessor :hash; end    # Trie.hash&lt;br /&gt;&lt;br /&gt;   def initialize&lt;br /&gt;      Trie.hash.merge_nested_hash!({:root=&gt;{}})&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   def add_int(int)   # for int &gt;= 0&lt;br /&gt;      ia = int.to_s.scan(/[[:digit:]]/).map { |i| i.to_i }  # integer array; ex: [4,6,2]&lt;br /&gt;      ia.unshift(:root).push(true)&lt;br /&gt;      Trie.hash.merge_nested_hash!(Hash.new.nested_hash(ia))&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def matchi(int)  # match integer&lt;br /&gt;      ia = int.to_s.scan(/[[:digit:]]/).map { |i| i.to_i }  &lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ia.each do |digit|&lt;br /&gt;         node = node[digit]&lt;br /&gt;         #node = node.fetch(digit,nil)&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      node.fetch(true,nil) ? true : false&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def mfpi(int)   # match first part of integer&lt;br /&gt;      ia = int.to_s.scan(/[[:digit:]]/).map { |i| i.to_i }&lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ia.each do |digit|&lt;br /&gt;         node = node[digit]&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      return true&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   def add_word(word)&lt;br /&gt;      ca = word.split(//u)    # UTF-8 aware character array&lt;br /&gt;      ca.unshift(:root).push(true)&lt;br /&gt;      Trie.hash.merge_nested_hash!(Hash.new.nested_hash(ca))&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def match(word)   # match entire word&lt;br /&gt;      ca = word.split(//u)    &lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ca.each do |char|&lt;br /&gt;         node = node[char]&lt;br /&gt;         #node = node.fetch(char,nil)&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      node.fetch(true,nil) ? true : false&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def mfpw(word)   # match first part of word&lt;br /&gt;      ca = word.split(//u)    &lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ca.each do |char|&lt;br /&gt;         node = node[char]&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      return true&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;trie = Trie.new&lt;br /&gt;p Trie.hash&lt;br /&gt;&lt;br /&gt;trie.add_word("word")&lt;br /&gt;pp Trie.hash            &lt;br /&gt;p trie.match("word")      &lt;br /&gt;p trie.match("word2")   &lt;br /&gt;trie.add_word("word2")&lt;br /&gt;p trie.match("word2")   &lt;br /&gt;pp Trie.hash            # {:root=&gt;{"w"=&gt;{"o"=&gt;{"r"=&gt;{"d"=&gt;{true=&gt;{}, "2"=&gt;{true=&gt;{}}}}}}}}&lt;br /&gt;&lt;br /&gt;trie.add_word("")&lt;br /&gt;p trie.match("")&lt;br /&gt;pp Trie.hash&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;p trie.match("word")      &lt;br /&gt;p trie.mfpw("wor")     # match first part of word&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;trie.add_int(12345)&lt;br /&gt;p Trie.hash&lt;br /&gt;&lt;br /&gt;trie.add_int(12980345)&lt;br /&gt;pp Trie.hash&lt;br /&gt;&lt;br /&gt;trie.add_int(8512)&lt;br /&gt;pp Trie.hash&lt;br /&gt;p trie.matchi(8512)&lt;br /&gt;p trie.matchi(85)&lt;br /&gt;p trie.mfpi(85)     # match first part of integer&lt;br /&gt;p trie.matchi(51)&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;pp Trie.hash&lt;br /&gt;paths = []&lt;br /&gt;Trie.hash.each_trie_path { |path, value| paths.push([ path ]) }&lt;br /&gt;#Trie.hash.each_trie_path { |path, value| paths.push([ path, value ]) }&lt;br /&gt;#pp paths&lt;br /&gt;puts&lt;br /&gt;paths.each { |x| p x }&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;require 'pp'&lt;br /&gt;&lt;br /&gt;class Hash&lt;br /&gt;   def Hash.new_nested_hash&lt;br /&gt;     Hash.new{|h,k| h[k]=Hash.new(&amp;h.default_proc) }&lt;br /&gt;   end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Trie&lt;br /&gt;&lt;br /&gt;   @hash = Hash.new_nested_hash&lt;br /&gt;   #@hash = Hash.new_nested_hash.update(true=&gt;{})  # add empty string by default&lt;br /&gt;   class &lt;&lt; self; attr_accessor :hash; end    # Trie.hash&lt;br /&gt;&lt;br /&gt;   def &lt;&lt;(word)&lt;br /&gt;      ca = word.split(//u)    # UTF-8 aware character array&lt;br /&gt;      wl = ca.size            # word length&lt;br /&gt;      str = ""&lt;br /&gt;      wl.times { |x| str &lt;&lt; "[ca.at(#{x})]" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; "[true]"&lt;br /&gt;      #p str     # example: "Trie.hash[ca.at(0)][ca.at(1)][ca.at(2)][ca.at(3)][true]"&lt;br /&gt;      eval(str)&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   def match(word)&lt;br /&gt;      ca = word.split(//u)    # UTF-8 aware character array&lt;br /&gt;      wl = ca.size            # word length&lt;br /&gt;      str = ""&lt;br /&gt;&lt;br /&gt;      wl.times { |x| str &lt;&lt; ".fetch(ca.at(#{x}),nil)" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; ".fetch(true,nil)"&lt;br /&gt;      #p str   # example: "Trie.hash.fetch(ca.at(0),nil).fetch(ca.at(1),nil).fetch(ca.at(2),nil).fetch(true,nil)"&lt;br /&gt;      ret = eval(str) rescue nil&lt;br /&gt;&lt;br /&gt;=begin&lt;br /&gt;      # alternative&lt;br /&gt;      wl.times { |x| str &lt;&lt; ".fetch(ca.at(#{x}),{})" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; ".fetch(true,nil)"&lt;br /&gt;      #p str   # example: "Trie.hash.fetch(ca.at(0),{}).fetch(ca.at(1),{}).fetch(ca.at(2),{}).fetch(true,nil)"&lt;br /&gt;      ret = eval(str)&lt;br /&gt;=end&lt;br /&gt;&lt;br /&gt;      ret == {} ? true : false   # {} is the default value created by Trie.hash or Hash.new_nested_hash respectively&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;trie = Trie.new&lt;br /&gt;trie &lt;&lt; "word"&lt;br /&gt;pp Trie.hash            &lt;br /&gt;p trie.match("word")      &lt;br /&gt;p trie.match("word2")   &lt;br /&gt;trie &lt;&lt; "word2"&lt;br /&gt;p trie.match("word2")   &lt;br /&gt;pp Trie.hash            # {"w"=&gt;{"o"=&gt;{"r"=&gt;{"d"=&gt;{true=&gt;{}, "2"=&gt;{true=&gt;{}}}}}}}&lt;br /&gt;&lt;br /&gt;trie &lt;&lt; ""&lt;br /&gt;p trie.match("")&lt;br /&gt;pp Trie.hash&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# alternative with default :root element&lt;br /&gt;&lt;br /&gt;require 'pp'&lt;br /&gt;&lt;br /&gt;class Hash&lt;br /&gt;   def Hash.new_nested_hash&lt;br /&gt;     Hash.new{|h,k| h[k]=Hash.new(&amp;h.default_proc) }&lt;br /&gt;   end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Hash&lt;br /&gt;&lt;br /&gt;   # code basis taken from: "Find every path and it's value in a Hash" by Florian A&#223;mann,&lt;br /&gt;   # http://snippets.dzone.com/posts/show/3565&lt;br /&gt;&lt;br /&gt;   def each_trie_path&lt;br /&gt;      raise ArgumentError unless block_given?&lt;br /&gt;      self.class.each_trie_path(self) { |path, object| yield(path, object) }&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   protected&lt;br /&gt;   def self.each_trie_path(object, path = [], &amp;block)  &lt;br /&gt;&lt;br /&gt;      if object.is_a?(Hash)&lt;br /&gt;         object.each do |key, value|&lt;br /&gt;&lt;br /&gt;            if key == true &amp;&amp; value == {}&lt;br /&gt;               if path == [:root]  # special case for empty string: [[:root], {true=&gt;{}}]&lt;br /&gt;                  yield(path, {true=&gt;{}})&lt;br /&gt;                  next&lt;br /&gt;               end&lt;br /&gt;               yield(path, object)&lt;br /&gt;            end&lt;br /&gt;&lt;br /&gt;            self.each_trie_path(value, [path , key].flatten, &amp;block)&lt;br /&gt;         end&lt;br /&gt;      else &lt;br /&gt;         yield(path, object)&lt;br /&gt;      end&lt;br /&gt;   end &lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Trie&lt;br /&gt;&lt;br /&gt;   @hash = Hash.new_nested_hash&lt;br /&gt;   class &lt;&lt; self; attr_accessor :hash; end    # Trie.hash&lt;br /&gt;&lt;br /&gt;   def initialize&lt;br /&gt;      Trie.hash[:root]&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   def add_int(int)   # for int &gt;= 0&lt;br /&gt;      ia = int.to_s.scan(/[[:digit:]]/).map { |i| i.to_i }  # integer array; ex: [4,6,2]&lt;br /&gt;      ia.unshift(:root)&lt;br /&gt;      str = ""&lt;br /&gt;      ia.size.times { |x| str &lt;&lt; "[ia.at(#{x})]" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; "[true]"&lt;br /&gt;      eval(str)&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def matchi(int)  # match integer&lt;br /&gt;      ia = int.to_s.scan(/[[:digit:]]/).map { |i| i.to_i }  &lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ia.each do |digit|&lt;br /&gt;         node = node.fetch(digit,nil)&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      node.fetch(true,nil) ? true : false&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def mfpi(int)   # match first part of integer&lt;br /&gt;      ia = int.to_s.scan(/[[:digit:]]/).map { |i| i.to_i }&lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ia.each do |digit|&lt;br /&gt;         node = node.fetch(digit,nil)&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      return true&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   def &lt;&lt;(word)&lt;br /&gt;      ca = word.split(//u)    # UTF-8 aware character array&lt;br /&gt;      ca.unshift(:root)&lt;br /&gt;      #ca = [:root, *word.split(//u)]&lt;br /&gt;      #ca = [:root].concat(word.split(//u))&lt;br /&gt;      str = ""&lt;br /&gt;      ca.size.times { |x| str &lt;&lt; "[ca.at(#{x})]" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; "[true]"&lt;br /&gt;      #p str     # example: "Trie.hash[ca.at(0)][ca.at(1)][ca.at(2)][ca.at(3)][true]"&lt;br /&gt;      eval(str)&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def match(word)&lt;br /&gt;      ca = word.split(//u)    &lt;br /&gt;      ca.unshift(:root)&lt;br /&gt;      #ca = [:root, *word.split(//u)]&lt;br /&gt;      #ca = [:root].concat(word.split(//u))&lt;br /&gt;      str = ""&lt;br /&gt;      ca.size.times { |x| str &lt;&lt; ".fetch(ca.at(#{x}),nil)" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; ".fetch(true,nil)"&lt;br /&gt;      #p str   # example: "Trie.hash.fetch(ca.at(0),nil).fetch(ca.at(1),nil).fetch(ca.at(2),nil).fetch(true,nil)"&lt;br /&gt;      ret = eval(str) rescue nil&lt;br /&gt;&lt;br /&gt;=begin&lt;br /&gt;      # alternative&lt;br /&gt;      ca.size.times { |x| str &lt;&lt; ".fetch(ca.at(#{x}),{})" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; ".fetch(true,nil)"&lt;br /&gt;      #p str   # example: "Trie.hash.fetch(ca.at(0),{}).fetch(ca.at(1),{}).fetch(ca.at(2),{}).fetch(true,nil)"&lt;br /&gt;      ret = eval(str)&lt;br /&gt;=end&lt;br /&gt;&lt;br /&gt;      ret == {} ? true : false   # {} is the default value created by Trie.hash or Hash.new_nested_hash respectively&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def match2(word)   # match entire word&lt;br /&gt;      ca = word.split(//u)    &lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ca.each do |char|&lt;br /&gt;         node = node.fetch(char,nil)&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      node.fetch(true,nil) ? true : false&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def mfpw(word)   # match first part of word&lt;br /&gt;      ca = word.split(//u)    &lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ca.each do |char|&lt;br /&gt;         node = node.fetch(char,nil)&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      return true&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;trie = Trie.new&lt;br /&gt;trie &lt;&lt; "word"&lt;br /&gt;pp Trie.hash            &lt;br /&gt;p trie.match("word")      &lt;br /&gt;p trie.match("word2")   &lt;br /&gt;trie &lt;&lt; "word2"&lt;br /&gt;p trie.match("word2")   &lt;br /&gt;pp Trie.hash            # {:root=&gt;{"w"=&gt;{"o"=&gt;{"r"=&gt;{"d"=&gt;{true=&gt;{}, "2"=&gt;{true=&gt;{}}}}}}}}&lt;br /&gt;&lt;br /&gt;trie &lt;&lt; ""&lt;br /&gt;p trie.match("")&lt;br /&gt;pp Trie.hash&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;p trie.match2("word")      &lt;br /&gt;p trie.mfpw("wor")     # match first part of word&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;trie.add_int(8512)&lt;br /&gt;pp Trie.hash&lt;br /&gt;p trie.matchi(8512)&lt;br /&gt;p trie.matchi(85)&lt;br /&gt;p trie.mfpi(85)     # match first part of integer&lt;br /&gt;p trie.matchi(51)&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;pp Trie.hash&lt;br /&gt;paths = []&lt;br /&gt;Trie.hash.each_trie_path { |path, value| paths.push([ path ]) }&lt;br /&gt;#Trie.hash.each_trie_path { |path, value| paths.push([ path, value ]) }&lt;br /&gt;#pp paths&lt;br /&gt;puts&lt;br /&gt;paths.each { |x| p x }&lt;br /&gt;puts&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 07 Jun 2008 20:54:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5616</guid>
      <author>ntk ()</author>
    </item>
    <item>
      <title>Matching quoted strings in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5459</link>
      <description>An exercise in string processing and regexp matching, inspired by &lt;a href="http://mikenaberezny.com/2008/04/28/parsing-quoted-strings-in-ruby/"&gt;Parsing Quoted Strings in Ruby&lt;/a&gt; and &lt;a href="http://pivots.pivotallabs.com/users/nick/blog/articles/409-stupid-ruby-quoting-tricks/"&gt;Stupid Ruby Quoting Tricks&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/local/bin/ruby -w&lt;br /&gt;&lt;br /&gt;# some input examples&lt;br /&gt;str = 'foo "bar baz" qux'&lt;br /&gt;str = 'foo "bar baz " "bar baz" " bar baz" "bar "klr mre" " " \' "abc" \' baz " qux'&lt;br /&gt;str = '" \' \'    " \n "   " \' \' "" foo \'ttt sss\' "bar "qqq zzz" baz" "added term" qux  " \' \'    "  yyy xxx'&lt;br /&gt;str = '"""frickin \'#{bar}\'"""'&lt;br /&gt;str = '""    "frickin chicken "    #{bar}""""'&lt;br /&gt;str = '"""frickin "#{bar}""""'&lt;br /&gt;str = '"a "b c" "d "e" f g" """h""""'       # cf. http://snippets.dzone.com/posts/show/4852&lt;br /&gt;&lt;br /&gt;# escaped quotes&lt;br /&gt;str = '\"'&lt;br /&gt;str = "\\\""&lt;br /&gt;str = '\\\''&lt;br /&gt;str = "\\'"&lt;br /&gt;&lt;br /&gt;# special cases&lt;br /&gt;str = '"G","H I"'&lt;br /&gt;str = '"G","H I""G","H I"'&lt;br /&gt;&lt;br /&gt;str = '"abc""def"'&lt;br /&gt;str = '"""a""b"'&lt;br /&gt;str = '"abc""def""abc""def""abc""def"'&lt;br /&gt;str = '"a"\'\'"b"'&lt;br /&gt;&lt;br /&gt;str = "\"abc'vv'tt\"'klt'"&lt;br /&gt;&lt;br /&gt;str = "abc,def,\"efg,hij\",klm,nop,\"qrstuv\",wxyz"&lt;br /&gt;str = "abc,def,\"efg,hij\",klm, 'nop, \"qrstuv\",wxyz,mmm '"&lt;br /&gt;str = "abc,def,\"efg,hij\",klm, \"nop, 'qrstuv',wxyz,mmm \""&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;puts "input string:  #{str}" &lt;br /&gt;puts "str.inspect :  #{str.inspect}" &lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;num_of_chars1 = str.count('a-zA-Z_0-9', "^\000ds")&lt;br /&gt;&lt;br /&gt;error_code = 0      # in case of a parsing error Shellwords will be used instead of regex1 &amp; regex2&lt;br /&gt;str2 = str.clone&lt;br /&gt;&lt;br /&gt;# encode escaped quotes&lt;br /&gt;str = str.gsub(/\\"|\\'/) { |m| m =~ /^\\"$/ ? "\000d\000" : "\000s\000" }&lt;br /&gt;&lt;br /&gt;dq_count = str.count('"')&lt;br /&gt;sq_count = str.count("'")&lt;br /&gt;&lt;br /&gt;if dq_count % 2 != 0 &amp;&amp; sq_count % 2 != 0&lt;br /&gt;   raise ArgumentError, "\e[1modd number of single &amp; double quotes\e[m in: #{str}\nsq_count: #{sq_count}\ndq_count: #{dq_count}\n"&lt;br /&gt;elsif dq_count % 2 != 0&lt;br /&gt;   raise ArgumentError, "\e[1modd number of double quotes\e[m in: #{str}\ndq_count: #{dq_count}\n"&lt;br /&gt;elsif sq_count % 2 != 0&lt;br /&gt;   raise ArgumentError, "\e[1modd number of single quotes\e[m in: #{str}\nsq_count: #{sq_count}\n"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# regex1 separates substrings that contain quotes from substrings that do not contain quotes&lt;br /&gt;regex1 = %r{[^"']+|["'].*?["'](?!.*["'])}m  &lt;br /&gt;&lt;br /&gt;# example&lt;br /&gt;#"abc 'quote1' pjk 'quote2' xyz".scan(regex1) { |m| puts m } &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;regex2 = %r{&lt;br /&gt;# experimental: special cases&lt;br /&gt;\s*["'][^"']+["'][[:punct:]]["'][^"']+["']|  # special case:  xxx "ab c","def g" yyy&lt;br /&gt;\s*["'][^"']+["']{2,}[^"']+["']|             # special case:  xxx "abc""def" yyy&lt;br /&gt;\s"[^"]+"|                                   # special case: xxx "abc 'def' ghi"&lt;br /&gt;\s'[^']+'|                                   # special case: xxx 'abc "def" ghi'&lt;br /&gt;\s*["']\S+["']|                              # special case: "abc'vv'tt"'klt'&lt;br /&gt;&lt;br /&gt;\s'\s|                       # xxx ' yyy&lt;br /&gt;\s"\s|                       # xxx " yyy&lt;br /&gt;\s''\s|                      # xxx '' yyy&lt;br /&gt;\s""\s|                      # xxx "" yyy&lt;br /&gt;\s'\s+'\s|                   # xxx '   ' yyy&lt;br /&gt;\s"\s+"\s|                   # xxx "   " yyy&lt;br /&gt;\s"\s[^"]+\s"\s|             # xxx " abc " yyy&lt;br /&gt;\s'\s[^']+\s'\s|             # xxx ' abc ' yyy&lt;br /&gt;\s["']["']+(?=[^"'\s])|      # :qoblock:  xxx "'""'abc yyy&lt;br /&gt;[^"'\s]["']["']+(?=\s)|      # :qcblock:  xxx abc"'""' yyy&lt;br /&gt;\s""+|                       # :dqoblock:  xxx """abc yyy&lt;br /&gt;\s''+|                       # :sqoblock:  xxx '''abc yyy&lt;br /&gt;[^"]""+|                     # :dqcblock:  xxx abc"" yyy&lt;br /&gt;[^']''+|                     # :sqcblock:  xxx abc'' yyy&lt;br /&gt;\s["'](?=[^"'\s])|           # :dqo or :sqo:  xxx "abc yyy  or  xxx 'abc yyy&lt;br /&gt;[^"'\s]["'](?=\s)|           # :dqc or :sqc:  xxx abc" yyy  or  xxx abc' yyy&lt;br /&gt;[^"']+[^"'\s](?=\s)          # no quotes at all&lt;br /&gt;}mx&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;=begin&lt;br /&gt;&lt;br /&gt;There are different kinds of quotes matched by regex2 below. They include:&lt;br /&gt;&lt;br /&gt;- :sqo (single quote open)&lt;br /&gt;- :sqc (single quote close)&lt;br /&gt;- :sqoblock (single quote open block)&lt;br /&gt;- :sqcblock (single quote close block)&lt;br /&gt;&lt;br /&gt;- :dqo (double quote open)&lt;br /&gt;- :dqc (double quote close)&lt;br /&gt;- :dqoblock (double quote open block)&lt;br /&gt;- :dqcblock (double quote close block)&lt;br /&gt;&lt;br /&gt;- :qoblock (quote open block)&lt;br /&gt;- :qcblock (quote close block)&lt;br /&gt;&lt;br /&gt;=end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ret = []&lt;br /&gt;&lt;br /&gt;str.scan(regex1) do |s| &lt;br /&gt;&lt;br /&gt;   if s !~ /\A["']/&lt;br /&gt;&lt;br /&gt;      #puts "s1: #{s}"&lt;br /&gt;      #puts "s1.inspect: #{s.inspect}"&lt;br /&gt;&lt;br /&gt;      s.split(/\s+/m).each { |t| ret &lt;&lt; t unless t.empty? }&lt;br /&gt;&lt;br /&gt;   else&lt;br /&gt;&lt;br /&gt;      #puts "s2: #{s}"&lt;br /&gt;      #puts "s2.inspect: #{s.inspect}"&lt;br /&gt;&lt;br /&gt;      open_quotes = 0&lt;br /&gt;      close_quotes = 0&lt;br /&gt;      ar = []&lt;br /&gt;&lt;br /&gt;      # add spaces to simplify regex2 matching&lt;br /&gt;      s = "\x20" &lt;&lt; s &lt;&lt; "\x20"    &lt;br /&gt;      s.gsub!(/\x20/, "\x20\x20")  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;      s.scan(regex2) do |m|&lt;br /&gt;&lt;br /&gt;         # get the index of the quote&lt;br /&gt;         # + 1 for leading space or non-space&lt;br /&gt;         # $` is the prematch string&lt;br /&gt;&lt;br /&gt;         index = $`.length + 1 &lt;br /&gt;&lt;br /&gt;         post_match = $'  &lt;br /&gt;&lt;br /&gt;         #puts&lt;br /&gt;         #puts "index: #{index}"&lt;br /&gt;         #puts "m: #{m.inspect}"&lt;br /&gt;         #puts "m.length: #{m.length}"&lt;br /&gt;         #puts "open_quotes:  #{open_quotes}\nclose_quotes: #{close_quotes}"&lt;br /&gt;         #puts "ret: #{ret.inspect}"&lt;br /&gt;         #puts "ar: #{ar.inspect}"&lt;br /&gt;         #puts&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;         if m =~ /\A\s''\s\z/&lt;br /&gt;&lt;br /&gt;            next unless open_quotes == 0 &amp;&amp; close_quotes == 0&lt;br /&gt;            ret &lt;&lt; ''&lt;br /&gt;            next&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A\s""\s\z/&lt;br /&gt;&lt;br /&gt;            next unless open_quotes == 0 &amp;&amp; close_quotes == 0&lt;br /&gt;            ret &lt;&lt; ""&lt;br /&gt;            next&lt;br /&gt;&lt;br /&gt;         # example: xxx "ab c","def g" yyy&lt;br /&gt;         elsif open_quotes.zero? &amp;&amp; close_quotes.zero? &amp;&amp; m =~ /\A\s*["'][^"']+["'][[:punct:]]["'][^"']+["']\z/ &amp;&amp; m.count('"') % 2 == 0 &amp;&amp; m.count("'") % 2 == 0           &lt;br /&gt;&lt;br /&gt;            m = m.gsub(/\x20\x20/, "\x20")&lt;br /&gt;            # cf. http://henrik.nyh.se/2008/03/flickr-style-tag-splitting-in-ruby&lt;br /&gt;            m = m.split(/"(.+?)"|\s+/).reject {|sm| sm.empty? }&lt;br /&gt;            #m = m.split(/"(.+?)"|'(.+?)'|\s+/).reject {|sm| sm.empty? }&lt;br /&gt;            #m = m.split(/"(.+?)"|'(.+?)'|([[:punct:]])|\s+/).reject {|sm| sm.empty? }&lt;br /&gt;            ret.concat(m)&lt;br /&gt;            next&lt;br /&gt;&lt;br /&gt;         # example: xxx "abc""def" yyy&lt;br /&gt;         elsif open_quotes.zero? &amp;&amp; close_quotes.zero? &amp;&amp; m =~ /\A\s*["'][^"']+["']{2,}[^"']+["']\z/ &amp;&amp; m.count('"') % 2 == 0 &amp;&amp; m.count("'") % 2 == 0           &lt;br /&gt;            &lt;br /&gt;            m = m.gsub(/\x20\x20/, "\x20")&lt;br /&gt;            m = m.split(/"(.+?)"|\s+/).reject {|sm| sm.empty? }&lt;br /&gt;            #m = m.split(/"(.+?)"|'(.+?)'|\s+/).reject {|sm| sm.empty? }&lt;br /&gt;            ret.concat(m)&lt;br /&gt;            next&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;         elsif open_quotes.zero? &amp;&amp; close_quotes.zero? &amp;&amp; m =~ /\A\s"[^"]+"\z/ &amp;&amp; m.count('"') % 2 == 0 &amp;&amp; m.count("'") % 2 == 0&lt;br /&gt;            ret.concat(m.split(/"(.+?)"|\s+/).reject {|sm| sm.empty? })&lt;br /&gt;            next&lt;br /&gt;&lt;br /&gt;         elsif open_quotes.zero? &amp;&amp; close_quotes.zero? &amp;&amp; m =~ /\A\s'[^']+'\z/ &amp;&amp; m.count('"') % 2 == 0 &amp;&amp; m.count("'") % 2 == 0&lt;br /&gt;            ret.concat(m.split(/'(.+?)'|\s+/).reject {|sm| sm.empty? })&lt;br /&gt;            next&lt;br /&gt;&lt;br /&gt;         elsif open_quotes.zero? &amp;&amp; close_quotes.zero? &amp;&amp; m =~ /\A\s*["']\S+["']\z/ &amp;&amp; m.count('"') % 2 == 0 &amp;&amp; m.count("'") % 2 == 0&lt;br /&gt;            ret.concat(m.split(/"(.+?)"|\s+/).reject {|sm| sm.empty? })&lt;br /&gt;            next&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A\s"\s[^"]+\s"\s\z/&lt;br /&gt;&lt;br /&gt;            next unless open_quotes == 0 &amp;&amp; close_quotes == 0&lt;br /&gt;            ret &lt;&lt; m.gsub(/\x20\x20/, "\x20").strip[1..-2]&lt;br /&gt;            next&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A\s'\s[^']+\s"\s\z/&lt;br /&gt;&lt;br /&gt;            next unless open_quotes == 0 &amp;&amp; close_quotes == 0&lt;br /&gt;            ret &lt;&lt; m.gsub(/\x20\x20/, "\x20").strip[1..-2]&lt;br /&gt;            next&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A\s'\s+'\s\z/&lt;br /&gt;&lt;br /&gt;            next unless open_quotes == 0 &amp;&amp; close_quotes == 0&lt;br /&gt;            ret &lt;&lt; m.gsub(/\x20\x20/, "\x20").strip[1..-2]&lt;br /&gt;            next&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A\s"\s+"\s\z/&lt;br /&gt;&lt;br /&gt;           next unless open_quotes == 0 &amp;&amp; close_quotes == 0&lt;br /&gt;           ret &lt;&lt; m.gsub(/\x20\x20/, "\x20").strip[1..-2]&lt;br /&gt;           next&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A\s""+\z/&lt;br /&gt;&lt;br /&gt;            l = m.strip.length&lt;br /&gt;            ar &lt;&lt; [:dqoblock, index, l]&lt;br /&gt;            old_open_quotes = open_quotes&lt;br /&gt;            open_quotes += l&lt;br /&gt;&lt;br /&gt;            if close_quotes == 0 &amp;&amp; old_open_quotes == 0 &amp;&amp; open_quotes % 2 == 0 &amp;&amp; post_match !~ /"/&lt;br /&gt;               ret &lt;&lt; m[2..-2] &lt;br /&gt;               open_quotes = 0&lt;br /&gt;               ar.pop&lt;br /&gt;               next&lt;br /&gt;            end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A\s''+\z/&lt;br /&gt;&lt;br /&gt;            l = m.strip.length&lt;br /&gt;            ar &lt;&lt; [:sqoblock, index, l]&lt;br /&gt;            old_open_quotes = open_quotes&lt;br /&gt;            open_quotes += l&lt;br /&gt;&lt;br /&gt;            if close_quotes == 0 &amp;&amp; old_open_quotes == 0 &amp;&amp; open_quotes % 2 == 0 &amp;&amp; post_match !~ /'/&lt;br /&gt;               ret &lt;&lt; m[2..-2] &lt;br /&gt;               open_quotes = 0&lt;br /&gt;               ar.pop&lt;br /&gt;               next&lt;br /&gt;            end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A[^"]""+\z/&lt;br /&gt;&lt;br /&gt;            l = m[1..-1].strip.length&lt;br /&gt;            ar &lt;&lt; [:dqcblock, index+l-1, l]      #  index+l-1 is the index of the last closing quote: ''"'[']&lt;br /&gt;            old_close_quotes = close_quotes&lt;br /&gt;            close_quotes += l&lt;br /&gt;&lt;br /&gt;            if open_quotes == 0 &amp;&amp; old_close_quotes == 0 &amp;&amp; close_quotes % 2 == 0 &amp;&amp; post_match !~ /"/&lt;br /&gt;               ret &lt;&lt; m[2..-2] &lt;br /&gt;               close_quotes = 0&lt;br /&gt;               ar.pop&lt;br /&gt;               next&lt;br /&gt;            end&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A[^']''+\z/&lt;br /&gt;&lt;br /&gt;            l = m[1..-1].strip.length&lt;br /&gt;            ar &lt;&lt; [:sqcblock, index+l-1, l]&lt;br /&gt;            old_close_quotes = close_quotes&lt;br /&gt;            close_quotes += l&lt;br /&gt;&lt;br /&gt;            if open_quotes == 0 &amp;&amp; old_close_quotes == 0 &amp;&amp; close_quotes % 2 == 0 &amp;&amp; post_match !~ /'/&lt;br /&gt;               ret &lt;&lt; m[2..-2] &lt;br /&gt;               close_quotes = 0&lt;br /&gt;               ar.pop&lt;br /&gt;               next&lt;br /&gt;            end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A\s'\z/&lt;br /&gt;&lt;br /&gt;            ar &lt;&lt; [:sqo, index, 1]&lt;br /&gt;            open_quotes += 1&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A\S'\z/&lt;br /&gt;&lt;br /&gt;            ar &lt;&lt; [:sqc, index, 1]&lt;br /&gt;            close_quotes += 1&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A\s"\z/&lt;br /&gt;&lt;br /&gt;            ar &lt;&lt; [:dqo, index, 1]&lt;br /&gt;            open_quotes += 1&lt;br /&gt;&lt;br /&gt;         elsif m =~ /\A\S"\z/&lt;br /&gt;&lt;br /&gt;            ar &lt;&lt; [:dqc, index, 1]&lt;br /&gt;            close_quotes += 1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;         else&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;            if m =~ /\A\s"\s\z/              # " surrounded by whitespace&lt;br /&gt;&lt;br /&gt;               if open_quotes &gt; close_quotes&lt;br /&gt;&lt;br /&gt;                  ar &lt;&lt; [:dqc, index, 1]&lt;br /&gt;                  close_quotes += 1&lt;br /&gt;&lt;br /&gt;                  # avoid :sqo followed by :dqc or :sqc followed by :dqc&lt;br /&gt;                  if post_match =~ /"/ &amp;&amp; open_quotes == close_quotes &amp;&amp; (ar.at(-2).first == :sqo || ar.at(-2).first == :sqc)&lt;br /&gt;                     ar.pop&lt;br /&gt;                     ar &lt;&lt; [:dqo, index, 1]&lt;br /&gt;                     close_quotes -= 1&lt;br /&gt;                     open_quotes += 1&lt;br /&gt;                  end&lt;br /&gt;&lt;br /&gt;               else &lt;br /&gt;&lt;br /&gt;                  ar &lt;&lt; [:dqo, index, 1]&lt;br /&gt;                  open_quotes += 1&lt;br /&gt;&lt;br /&gt;               end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;            elsif m =~ /\A\s'\s\z/          # ' surrounded by whitespace&lt;br /&gt;&lt;br /&gt;               if open_quotes &gt; close_quotes&lt;br /&gt;&lt;br /&gt;                  ar &lt;&lt; [:sqc, index, 1]&lt;br /&gt;                  close_quotes += 1&lt;br /&gt;&lt;br /&gt;                  # avoid :dqo followed by :sqc or :dqc followed by :sqc&lt;br /&gt;                  if post_match =~ /'/ &amp;&amp; open_quotes == close_quotes &amp;&amp; (ar.at(-2).first == :dqo || ar.at(-2).first == :dqc)&lt;br /&gt;                     ar.pop&lt;br /&gt;                     ar &lt;&lt; [:sqo, index, 1]&lt;br /&gt;                     close_quotes -= 1&lt;br /&gt;                     open_quotes += 1&lt;br /&gt;                  end&lt;br /&gt;&lt;br /&gt;               else &lt;br /&gt;&lt;br /&gt;                  ar &lt;&lt; [:sqo, index, 1]&lt;br /&gt;                  open_quotes += 1&lt;br /&gt;&lt;br /&gt;               end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;            elsif m =~ /\A\s["']["']+\z/              # :qoblock: xxx "'""'abc yyy&lt;br /&gt;&lt;br /&gt;               l = m[1..-1].strip.length&lt;br /&gt;               ar &lt;&lt; [:qoblock, index, l]&lt;br /&gt;               old_open_quotes = open_quotes&lt;br /&gt;               open_quotes += l&lt;br /&gt;&lt;br /&gt;               if close_quotes == 0 &amp;&amp; old_open_quotes == 0 &amp;&amp; open_quotes % 2 == 0 &amp;&amp; post_match !~ /["']/&lt;br /&gt;                  ret &lt;&lt; m[2..-2] &lt;br /&gt;                  open_quotes = 0&lt;br /&gt;                  ar.pop&lt;br /&gt;                  next&lt;br /&gt;               end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;            elsif m =~ /\A[^"'\s]["']["']+\z/          # :qcblock: xxx abc"'""' yyy&lt;br /&gt;&lt;br /&gt;               l = m[1..-1].strip.length&lt;br /&gt;               ar &lt;&lt; [:qcblock, index+l-1, l]&lt;br /&gt;               old_close_quotes = close_quotes&lt;br /&gt;               close_quotes += l&lt;br /&gt;&lt;br /&gt;               if open_quotes == 0 &amp;&amp; old_close_quotes == 0 &amp;&amp; close_quotes % 2 == 0 &amp;&amp; post_match !~ /["']/&lt;br /&gt;                  ret &lt;&lt; m[2..-2] &lt;br /&gt;                  close_quotes = 0&lt;br /&gt;                  ar.pop&lt;br /&gt;                  next&lt;br /&gt;               end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;            elsif m =~ /\A\s*["'].*?["']\s*\z/       # last try  (experimental)&lt;br /&gt;&lt;br /&gt;               ret.concat(m.split(/"(.+?)"|\s+/).reject {|sm| sm.empty? })&lt;br /&gt;               next&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;            elsif m =~ /\A[^"']+[^"'\s]\z/          # part of quoted substring contains neither " nor '&lt;br /&gt;               next unless open_quotes == 0 &amp;&amp; close_quotes == 0&lt;br /&gt;               next if m.strip.empty?&lt;br /&gt;               ret &lt;&lt; m.gsub(/\x20\x20/, "\x20").strip; next&lt;br /&gt;            end&lt;br /&gt;&lt;br /&gt;         end&lt;br /&gt;&lt;br /&gt;         puts&lt;br /&gt;         puts "open_quotes:  #{open_quotes}\nclose_quotes: #{close_quotes}\n"&lt;br /&gt;         #puts "ar: #{ar.inspect}"&lt;br /&gt;&lt;br /&gt;         if open_quotes == close_quotes&lt;br /&gt;&lt;br /&gt;            #puts "open_quotes &amp; close_quotes: #{close_quotes}"&lt;br /&gt;            puts "ar: #{ar.inspect}"&lt;br /&gt;&lt;br /&gt;            ret &lt;&lt; s[ar.first[1]..ar.last[1]].gsub(/\x20\x20/, "\x20")[1..-2] unless ar.empty?&lt;br /&gt;&lt;br /&gt;            ar.clear&lt;br /&gt;            open_quotes = 0&lt;br /&gt;            close_quotes = 0&lt;br /&gt;&lt;br /&gt;         end&lt;br /&gt;&lt;br /&gt;      end   # scan 2&lt;br /&gt;&lt;br /&gt;      unless open_quotes.zero? &amp;&amp; close_quotes.zero?&lt;br /&gt;        error_code = 1&lt;br /&gt;        puts "\e[1mparsing error\e[m for the quoted string: #{str.strip.squeeze[0..20]}"&lt;br /&gt;        #raise "\e[1mparsing error\e[m for the quoted string: #{str.strip.squeeze[0..20]}"&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;   end   # if&lt;br /&gt;&lt;br /&gt;end   # scan 1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;num_of_chars2 = ret.join.count('a-zA-Z_0-9', "^\000ds")&lt;br /&gt;&lt;br /&gt;unless num_of_chars1 == num_of_chars2&lt;br /&gt;   error_code = 1&lt;br /&gt;   puts "\n\e[1mparsing error due to wrong number of characters a-zA-Z_0-9\e[m: \n#{num_of_chars2} instead of #{num_of_chars1}\n"&lt;br /&gt;   #raise "\e[1mparsing error due to wrong number of characters a-zA-Z_0-9\e[m: \n#{num_of_chars2} instead of #{num_of_chars1}\n in #{str.strip.squeeze[0..20]}"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# use Shellwords in case the quote matching above failed&lt;br /&gt;if error_code == 1       &lt;br /&gt;#if error_code == 1 || ret.join =~ /\A["']+\z/        &lt;br /&gt;   require 'shellwords'&lt;br /&gt;   ret.clear&lt;br /&gt;   ret.concat(Shellwords::shellwords(str))&lt;br /&gt;   #str =~ /\A\S+\z/ ? ret.concat(str.split(/"(.+?)"|\s+/).reject {|sm| sm.empty? }) : ret.concat(Shellwords::shellwords(str))&lt;br /&gt;end &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;puts "\n\e[1mResult\e[m:\n\n"&lt;br /&gt;ret.each_with_index do |t,i| &lt;br /&gt;   # decode encoded escaped quotes &lt;br /&gt;   t = t.gsub(/\000d\000|\000s\000/) { |m| m =~ /^\000d\000$/ ? '\"' : "\\'" }&lt;br /&gt;   puts "#{i+1}:  #{t.inspect}" &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;puts "\n\e[1mShellwords\e[m:\n\n"&lt;br /&gt;require 'shellwords'&lt;br /&gt;Shellwords::shellwords(str2).each_with_index { |t,i| puts "#{i+1}:  #{t.inspect}" }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#----------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# matching quoted strings using backreferences&lt;br /&gt;# See: Regexes in Depth: Advanced Quoted String Matching,&lt;br /&gt;# http://blog.stevenlevithan.com/archives/match-quoted-string&lt;br /&gt;&lt;br /&gt;str = '"abc"'&lt;br /&gt;&lt;br /&gt;regex = %r{(["'])([^"']*)(\1)}&lt;br /&gt;regex = %r{(["'])([^\1]*)(\1)}&lt;br /&gt;p regex&lt;br /&gt;&lt;br /&gt;str.scan(regex) { |m| p m; p $1 &lt;&lt; $2 &lt;&lt; $3 }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 04 May 2008 18:47:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5459</guid>
      <author>ntk ()</author>
    </item>
    <item>
      <title>Finding your match with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5157</link>
      <description>This example finds an email subject in a string and passes the value to a variable called 'subject'.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;subject = (/^Subject\: (.+)$/).match(email)[1]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Prior to this code I would have used the following:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  email[/^Subject\: (.+)$/]&lt;br /&gt;  subject = $1&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Reference: &lt;a href="http://snippets.dzone.com/posts/show/5152"&gt;Ruby SMTP Server - Save to Database&lt;/a&gt; [dzone.com]</description>
      <pubDate>Wed, 20 Feb 2008 09:20:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5157</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>bScan - Simple Web Aplications Scanner</title>
      <link>http://snippets.dzone.com/posts/show/5094</link>
      <description>// Web application scanner (ex: phpBB, myCMS, myBlog, mySite etc..) - Only in PHP !&lt;br /&gt;// Find XSS, sql injection, remote file inclusion&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#####################################################################################&lt;br /&gt;#	Black_H  / Nooz -- 30:01:07 &lt;br /&gt;#	Bl4ck.H&lt;&gt;gmail&lt;&gt;com&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;class BScan&lt;br /&gt;&lt;br /&gt;#####################################################################################&lt;br /&gt;#	Regex&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;@@space    = '([[:space:]]*)'&lt;br /&gt;&lt;br /&gt;@@userdat  = '('&lt;br /&gt;@@userdat += '(\$_SERVER\[([\'\"]*)HTTP_)|'&lt;br /&gt;@@userdat += '(\$_GET)|'&lt;br /&gt;@@userdat += '(\$_POST)|'&lt;br /&gt;@@userdat += '(\$_COOKIE)|'&lt;br /&gt;@@userdat += '(\$_REQUEST)|'&lt;br /&gt;@@userdat += '(\$_FILES)|'&lt;br /&gt;@@userdat += '(\$_ENV)|'&lt;br /&gt;@@userdat += '(\$_HTTP_COOKIE_VARS)|'&lt;br /&gt;@@userdat += '(\$_HTTP_ENV_VARS)|'&lt;br /&gt;@@userdat += '(\$_HTTP_GET_VARS)|'&lt;br /&gt;@@userdat += '(\$_HTTP_POST_FILES)|'&lt;br /&gt;@@userdat += '(\$_HTTP_POST_VARS)|'&lt;br /&gt;@@userdat += '(\$_HTTP_SERVER_VARS\[([\'\"]*)HTTP_)'&lt;br /&gt;@@userdat += ')'&lt;br /&gt;&lt;br /&gt;@@regex = Hash.new&lt;br /&gt;@@regex = &lt;br /&gt;	{'TYPE' =&gt; 'vars overwrite','LEVEL' =&gt; '2','REGEX' =&gt; /extract#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'vars overwrite','LEVEL' =&gt; '2','REGEX' =&gt; /import_request_variables#{@@space}\((.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'fopen vuln','LEVEL' =&gt; '3','REGEX' =&gt; /fopen#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'copy vuln','LEVEL' =&gt; '3','REGEX' =&gt; /copy#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'fwrite vuln','LEVEL' =&gt; '3','REGEX' =&gt; /fwrite#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'sql injection','LEVEL' =&gt; '2','REGEX' =&gt; /(mysql_query|mssql_query|mysqli_query)#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'crlf injection','LEVEL' =&gt; '1','REGEX' =&gt; /mail#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'cross site scripting','LEVEL' =&gt; '1','REGEX' =&gt; /\&lt;\?\=#{@@space}(.*)#{@@userdat}/i},&lt;br /&gt;	{'TYPE' =&gt; 'cross site scripting','LEVEL' =&gt; '1','REGEX' =&gt; /(print|echo|print_r|var_dump)#{@@space}(|\(|\")(.*)#{@@userdat}/i},&lt;br /&gt;	{'TYPE' =&gt; 'php code execution','LEVEL' =&gt; '3','REGEX' =&gt; /eval#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'php code execution','LEVEL' =&gt; '3','REGEX' =&gt; /file_put_contents#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'variable attribution', 'LEVEL' =&gt; '2','REGEX' =&gt; /(.*)\$#{@@userdat}(.*)/i},&lt;br /&gt;	{'TYPE' =&gt; 'chmod affectation','LEVEL' =&gt; '1','REGEX' =&gt; /chmod#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'file disclosure','LEVEL' =&gt; '2','REGEX' =&gt; /(readfile|file_get_contents|file)#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'file disclosure','LEVEL' =&gt; '2','REGEX' =&gt; /(show_source|highlight_file)#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'bzopen vuln','LEVEL' =&gt; '2','REGEX' =&gt; /bzopen#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'file deletion','LEVEL' =&gt; '2','REGEX' =&gt; /(rmdir|unlink|delete)#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'command execution','LEVEL' =&gt; '3','REGEX' =&gt; /(exec|system|passthru|shell_exec|proc_open|pcntl_exec)#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'buffer overflow','LEVEL' =&gt; '3','REGEX' =&gt; /(confirm_phpdoc_compiled|mssql_pconnect|mssql_connect|crack_opendict|snmpget|ibase_connect)#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'ip falsification','LEVEL' =&gt; '1','REGEX' =&gt; /(.*)(HTTP_CLIENT_IP|HTTP_X_FORWARDED_FOR|HTTP_PC_REMOTE_ADDR)(.*)/i},&lt;br /&gt;	{'TYPE' =&gt; 'putenv vuln','LEVEL' =&gt; '2','REGEX' =&gt; /putenv#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'full path disclosure','LEVEL' =&gt; '1','REGEX' =&gt; /(htmlentities|htmlspecialchars)#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'magic_quotes_gpc bypass','LEVEL' =&gt; '1','REGEX' =&gt; /(stripslashes|urldecode)#{@@space}\((.*)#{@@userdat}(.*)\)/i},&lt;br /&gt;	{'TYPE' =&gt; 'file inclusion','LEVEL' =&gt; '3','REGEX' =&gt; /(include|include_once|require|require_once)#{@@space}(|\(|\")(.*)#{@@userdat}/i}&lt;br /&gt;&lt;br /&gt;#####################################################################################&lt;br /&gt;#	Main&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;  def initialize()&lt;br /&gt;&lt;br /&gt;	################&lt;br /&gt;	#	Usage&lt;br /&gt;&lt;br /&gt;if (ARGV.length &lt; 4)&lt;br /&gt;puts  '&lt;br /&gt; ---------------------------------------------------------------------&lt;br /&gt;|             Credits: Black_H &lt;bl4ck.h@gmail.com&gt;                    |&lt;br /&gt;|                 URL: Lemon-Inside.sup.fr                            |&lt;br /&gt;|                Note: Premier code Ruby                              |&lt;br /&gt; ---------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt; ---------------------------------------------------------------------&lt;br /&gt;|   Usage:  scan.rb -d &lt;Dossier&gt; -i &lt;Save.html&gt;                       |&lt;br /&gt;|   Ex:  scan.rb -d ./ -i output.html                                 |&lt;br /&gt; ---------------------------------------------------------------------		&lt;br /&gt; '&lt;br /&gt; end&lt;br /&gt; &lt;br /&gt;	################&lt;br /&gt;	#	Options &amp; Vars&lt;br /&gt;	&lt;br /&gt;	@@scan_alldir =  self.options('d')&lt;br /&gt;	@@out_file =  self.options('i')&lt;br /&gt;	&lt;br /&gt;	@@ban = [".", "..", "scan.rb", @@out_file.to_s]&lt;br /&gt;&lt;br /&gt;	@@scan_buffer = Array.new&lt;br /&gt;	&lt;br /&gt;	################&lt;br /&gt;	#	Options Error ?&lt;br /&gt;	&lt;br /&gt;	if (@@scan_alldir != false and @@scan_alldir.empty? == false)&lt;br /&gt;	self.dscan(@@scan_alldir)&lt;br /&gt;	self.output(@@scan_buffer)&lt;br /&gt;	@@scan_buffer = ''&lt;br /&gt;	end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	&lt;br /&gt;&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;#####################################################################################&lt;br /&gt;#	Dir Scan &lt;br /&gt;#&lt;br /&gt;  &lt;br /&gt;  def dscan(dir)&lt;br /&gt;      &lt;br /&gt;	d = Dir.open(dir.to_s)&lt;br /&gt;	d = d.sort - @@ban&lt;br /&gt;	&lt;br /&gt;      d.each { |fichier|&lt;br /&gt;&lt;br /&gt;      case File.ftype(dir+fichier)&lt;br /&gt;        when "directory"&lt;br /&gt;          self.dscan(dir + fichier + "/")&lt;br /&gt;        when "file"&lt;br /&gt;		  puts  'Scan =&gt; ' + dir + fichier &lt;br /&gt;          self.fscan(dir + fichier)&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;	  }&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;#####################################################################################&lt;br /&gt;#	File Scan &lt;br /&gt;#&lt;br /&gt;  &lt;br /&gt;  def fscan(file)&lt;br /&gt;&lt;br /&gt;	fichier = File.readlines(file)&lt;br /&gt;	i = 1&lt;br /&gt;&lt;br /&gt;	fichier.each { |line|&lt;br /&gt;						&lt;br /&gt;		@@regex.each  { |info|&lt;br /&gt;			&lt;br /&gt;			test = (line  =~ info['REGEX']) &lt;br /&gt;		&lt;br /&gt;				if (test) &lt;br /&gt;			&lt;br /&gt;				@@scan_buffer += ['FILE' =&gt; file, 'LINE' =&gt; i.to_s, 'MATCH' =&gt; line, 'LEVEL' =&gt; info['LEVEL'], 'TYPE' =&gt; info['TYPE']]&lt;br /&gt;				#	5 , 1 , 3 , 4 , 2&lt;br /&gt;				next @@scan_buffer&lt;br /&gt;				end&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;	i += 1&lt;br /&gt;  	} &lt;br /&gt;	&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;#####################################################################################&lt;br /&gt;#	Output buffer&lt;br /&gt;#&lt;br /&gt;  &lt;br /&gt;  def output(buffer)&lt;br /&gt;  &lt;br /&gt;	@html_hmodel = '&lt;html&gt;'&lt;br /&gt;	@html_hmodel += '&lt;style type="text/css"&gt;'&lt;br /&gt;	@html_hmodel += '&lt;!--'&lt;br /&gt;	@html_hmodel += '.level0 {background-color: #CCCCCC;}'&lt;br /&gt;	@html_hmodel += '.level1 {background-color: #33FF66;}'&lt;br /&gt;	@html_hmodel += '.level2 {background-color: #FFFF33;}'&lt;br /&gt;	@html_hmodel += '.level3 {background-color: #FF0000;}'&lt;br /&gt;	@html_hmodel += '--&gt;&lt;/style&gt;&lt;body&gt;&lt;h1&gt;BScan v1.0&lt;/h1&gt;&lt;pre&gt;'&lt;br /&gt;&lt;br /&gt;	code = @html_hmodel&lt;br /&gt;	&lt;br /&gt;	buffer.each { |infos|&lt;br /&gt;	&lt;br /&gt;	keys = infos.keys&lt;br /&gt;	code += "&lt;span class='level" + infos["LEVEL"] + "'&gt;" + keys[1].to_s + ' : ' + infos["TYPE"] + '&lt;/span&gt;&lt;br /&gt;'&lt;br /&gt;	code += "&lt;span class='" + infos["LEVEL"] + "'&gt;" + keys[3].to_s + ' : ' + infos["LEVEL"] + '&lt;/span&gt;&lt;br /&gt;'&lt;br /&gt;	code += "&lt;span class='" + infos["LEVEL"] + "'&gt;" + keys[4].to_s + ' : ' + infos["FILE"] + '&lt;/span&gt;&lt;br /&gt;'&lt;br /&gt;	code += "&lt;span class='" + infos["LEVEL"] + "'&gt;" + keys[0].to_s + ' : ' + infos["LINE"] + '&lt;/span&gt;&lt;br /&gt;'&lt;br /&gt;	code += "&lt;span class='" + infos["LEVEL"] + "'&gt;" + keys[2].to_s + ' : ' + infos["MATCH"] + '&lt;/span&gt;&lt;br /&gt;'&lt;br /&gt;	&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;		code += "&lt;/pre&gt;&lt;/body&gt;&lt;/html&gt;"&lt;br /&gt;		fhtml = File.open(@@out_file.to_s, "w")&lt;br /&gt;		fhtml.write code&lt;br /&gt;		code = ''&lt;br /&gt;&lt;br /&gt;	&lt;br /&gt;  end&lt;br /&gt;#####################################################################################&lt;br /&gt;#	Parse &amp; Get Options&lt;br /&gt;#&lt;br /&gt; &lt;br /&gt;  def options(param)&lt;br /&gt;  &lt;br /&gt;	i = 0&lt;br /&gt;		ARGV.each  { |valeur|&lt;br /&gt;		&lt;br /&gt;    		if (valeur == '-' + param.to_s)&lt;br /&gt;				return ARGV[i+1]&lt;br /&gt;			elseif (valeur != '-' + param.to_s)&lt;br /&gt;				return false&lt;br /&gt;			end&lt;br /&gt;		i += 1&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;	end&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;scan = BScan.new&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 03 Feb 2008 11:51:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5094</guid>
      <author>Black_H (Black_H)</author>
    </item>
    <item>
      <title>Automate the insertion of text into a file.</title>
      <link>http://snippets.dzone.com/posts/show/5032</link>
      <description>This example updates an xsl file with a new xsl:include declaration, and with a javascript header declaration. It reads 'guide.xsl' as a text file, and reads the template 'guide_ptemplate.xml' as a rexml document. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;#file: create add2guide_xsl.rb&lt;br /&gt;&lt;br /&gt;require 'rexml/document'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;class Add2GuideTxt&lt;br /&gt;  def initialize&lt;br /&gt;    @guide = 'guide.xsl'&lt;br /&gt;    @guide_template = 'guide_ptemplate.xml'&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def insertText(project)&lt;br /&gt;    # read guide.txt and return the buffer&lt;br /&gt;    buffer = readGuide(@guide)    &lt;br /&gt;    replaceBuffer(project, buffer, @guide) if not buffer.match("&lt;xsl:include href='/xsl/#{project}.xsl'/&gt;")&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def replaceBuffer(project, buffer, output_file)&lt;br /&gt;    # read guide_ptemplate.xsl and return the doc&lt;br /&gt;    doc = readGuideTemplate(@guide_template)    &lt;br /&gt;    # read the xsl:include&lt;br /&gt;    xsl_include = buildIncludeTemplate(project, doc)&lt;br /&gt;    # read the xsl:if&lt;br /&gt;    xsl_if = buildIfTemplate(project,doc)&lt;br /&gt;    &lt;br /&gt;    eoi = "&lt;!-- &lt;/xsl_includes&gt; --&gt;"&lt;br /&gt;    buffer = buffer.gsub(eoi, "#{xsl_include}\n" + eoi)&lt;br /&gt;    eoj = "&lt;!--&lt;/xsl_javascript&gt; --&gt;"&lt;br /&gt;    buffer = buffer.gsub(eoj, "\n#{xsl_if}\n" + eoj)&lt;br /&gt;    &lt;br /&gt;    file = File.new(output_file,'w')&lt;br /&gt;    file.puts buffer.gsub("&amp;quot;","'")&lt;br /&gt;    file.close&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def buildIncludeTemplate(project, doc)&lt;br /&gt;    xsl_include = doc.root.elements['xsl:include']&lt;br /&gt;    xsl_include.attributes['href'] = '/xsl/' + project + '.xsl'&lt;br /&gt;    xsl_include&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def buildIfTemplate(project, doc)&lt;br /&gt;    xsl_if = doc.root.elements['xsl:if']&lt;br /&gt;    xsl_if.attributes['test'] = project&lt;br /&gt;    xsl_if.elements['script[3]'].text = 'const ProjectX = "' + project + '";'&lt;br /&gt;    xsl_if&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def readGuide(filename) &lt;br /&gt;    file = File.new(filename,'r')&lt;br /&gt;    buffer = file.read&lt;br /&gt;    file.close&lt;br /&gt;    buffer&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def readGuideTemplate(filename) &lt;br /&gt;    file = File.new(filename)&lt;br /&gt;    Document.new(file)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0&lt;br /&gt;  a2g = Add2GuideTxt.new&lt;br /&gt;  #add the new project to the guide.xsl file  &lt;br /&gt;  a2g.insertText('tasks')&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;file: guide.xsl (target file)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!-- &lt;xsl_includes&gt; --&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- &lt;xsl_car_log.xsl/&gt; --&gt;&lt;br /&gt;&lt;xsl:include href="/xsl/car_log.xsl"/&gt;&lt;br /&gt;&lt;!-- &lt;/xsl_includes&gt; --&gt;&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;&lt;xsl:if test="car_logpage|car_log"&gt;&lt;br /&gt;  &lt;script type="text/javascript" src="/p/js/edit_recordx.js"&gt;&lt;/script&gt;&lt;br /&gt;  &lt;script type="text/javascript" src="/p/js/itasks5.js"&gt;&lt;/script&gt;&lt;br /&gt;  &lt;script type="text/javascript"&gt;const ProjectX = "car_log";&lt;/script&gt;&lt;br /&gt;&lt;/xsl:if&gt;&lt;br /&gt;&lt;!--&lt;/xsl_javascript&gt; --&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;file: guide_ptemplate.xml (template used to insert blocks of code into guide.xsl)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;template&gt;&lt;br /&gt;  &lt;xsl:include href="[x]"/&gt;&lt;br /&gt;  &lt;xsl:if test="[x]"&gt;&lt;br /&gt;    &lt;script type="text/javascript" src="/p/js/edit_recordx.js"&gt;&lt;/script&gt;&lt;br /&gt;    &lt;script type="text/javascript" src="/p/js/itasks5.js"&gt;&lt;/script&gt;&lt;br /&gt;    &lt;script type="text/javascript"&gt;[x]&lt;/script&gt;&lt;br /&gt;  &lt;/xsl:if&gt;&lt;br /&gt;&lt;/template&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 23 Jan 2008 15:19:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5032</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>words-like - Returns a block of words in the object that match the given pattern.</title>
      <link>http://snippets.dzone.com/posts/show/2752</link>
      <description>&lt;code&gt;&lt;br /&gt;words-like: func [&lt;br /&gt;    "Returns a block of words in the object that match the given pattern."&lt;br /&gt;    object  [object!]&lt;br /&gt;    pattern [word! any-string!]&lt;br /&gt;    ;??? Add an /unbound refinement&lt;br /&gt;][&lt;br /&gt;    pattern: join form pattern "*"&lt;br /&gt;    collect w [&lt;br /&gt;        foreach word next first object [&lt;br /&gt;            if find/match/any form word pattern [w: bind word in object 'self]&lt;br /&gt;        ]&lt;br /&gt;    ]&lt;br /&gt;]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 01 Oct 2006 22:25:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2752</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>C# Reflection - Dealing with AmbiguousMatchException</title>
      <link>http://snippets.dzone.com/posts/show/1984</link>
      <description>C# code invoking a static method with reflection and dealing with an AmbiguousMatchException&lt;br /&gt;Source: http://msdn2.microsoft.com/en-US/library/xthb9284.aspx&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;class Myambiguous {&lt;br /&gt;    //The first overload is typed to an Int32&lt;br /&gt;    public static void Mymethod (Int32 number){&lt;br /&gt;       Console.Write("\n{0}", "I am from Int32 method");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    //The second overload is typed to a string&lt;br /&gt;    public static void Mymethod (string alpha) {&lt;br /&gt;       Console.Write("\n{0}", "I am from a string.");&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    public static void Main() {&lt;br /&gt;        try {&lt;br /&gt;            //The following does not cause as exception&lt;br /&gt;            Mymethod (2);  // goes to Mymethod (Int32)&lt;br /&gt;            Mymethod ("3");   // goes to Mymethod (string)&lt;br /&gt;&lt;br /&gt;            Type Mytype = Type.GetType("Myambiguous");&lt;br /&gt;&lt;br /&gt;            MethodInfo Mymethodinfo32 = Mytype.GetMethod("Mymethod", new Type[]{typeof(Int32)});&lt;br /&gt;            MethodInfo Mymethodinfostr = Mytype.GetMethod("Mymethod", new Type[]{typeof(System.String)});&lt;br /&gt;&lt;br /&gt;            //Invoke a method, utilizing a Int32 integer&lt;br /&gt;            Mymethodinfo32.Invoke(null, new Object[]{2});&lt;br /&gt;&lt;br /&gt;            //Invoke the method utilizing a string&lt;br /&gt;            Mymethodinfostr.Invoke(null, new Object[]{"1"});&lt;br /&gt;&lt;br /&gt;            //The following line causes an ambiguious exception&lt;br /&gt;            MethodInfo Mymethodinfo = Mytype.GetMethod("Mymethod");&lt;br /&gt;        }  // end of try block&lt;br /&gt;  &lt;br /&gt;        catch(System.Reflection.AmbiguousMatchException theException) {&lt;br /&gt;            Console.Write("\nAmbiguousMatchException message - {0}", theException.Message);&lt;br /&gt;        }&lt;br /&gt;        catch {&lt;br /&gt;            Console.Write("\nError thrown");&lt;br /&gt;        }&lt;br /&gt;        return;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt; &lt;br /&gt; //This code produces the following output:&lt;br /&gt; //I am from Int32 method&lt;br /&gt; //I am from a string.&lt;br /&gt; //I am from Int32 method&lt;br /&gt; //I am from a string.&lt;br /&gt; //AmbiguousMatchException message - Ambiguous match found.&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 05 May 2006 17:15:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1984</guid>
      <author>Ozymandias ()</author>
    </item>
    <item>
      <title>Return ordered list of near match items based on tag matches</title>
      <link>http://snippets.dzone.com/posts/show/1445</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;select t2.dbid, t2.tag, count(t2.dbid) as match_count from tags as t1, tags as t2 where t1.dbid = '105319' and t2.tag = t1.tag and t1.dbid != t2.dbid GROUP BY t2.dbid 1 ORDER BY match_count;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Feb 2006 21:41:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1445</guid>
      <author>offspinner ()</author>
    </item>
  </channel>
</rss>
