INCLUDE_DATA

 
»
S
I
D
E
B
A
R
«
aquisto cialis levitra svizzera tadalafil bestellen viagra prix acheter du viagra acquista levitra cialis sur internet citrate de sildenafil compro viagra levitra ricetta kamagra pharmaceuticals cialis vente libre cialis suisse levitra italia vente de cialis viagra suisse kamagra gel viagra quanto costa cialis 20 mg prix cialis 10mg viagra verkauf cialis moins cher viagra prezzo levitra kopen acheter tadalafil acheter kamagra tadalafil generico impuissance sexuelle cialis preco levitra donna viagra 100 mg comprar cialis generico comprar levitra cialis a vendre procurer du cialis generische cialis vardenafil generico vardenafil generika levitra sur internet generische viagra acheter cialis generique acquista viagra achat cialis viagra 100 mg cialis generico kamagra en france viagra ordonnance acheter viagra achat vardenafil pastilla levitra viagra cialis differenze impotenza sessuale venta viagra medicament cialis curare impotenza kamagra te koop achat cialis 20mg levitra pharmacie cialis receta acquisto viagra net cialis en ligne achat de viagra cialis generique acheter acheter du cialis cialis 20 mg vardenafil 10 mg viagra alternativo citrate de sildenafil cialis sin receta viagra kopen acheter cialis en pharmacie kamagra bestellen comprar viagra pela internet viagra prescrizione levitra donne vente cialis venta de sildenafil achete levitra acheter cialis france venta de levitra viagra kosten cialis marche pas comprar vardenafil disfunzione erettile rimedi vardenafil generique viagra recensioni cialis generico prezzo viagra versand cialis europe viagra venta libre impotenza rimedi cialis rezeptfrei acheter kamagra france levitra ohne rezept acquisto viagra in contrassegno prix de cialis cialis prescrizione viagra acquisto online achat viagra pildoras cialis kamagra generique cialis prezzo cialis inde cialis sur le net acheter cialis en espagne levitra ordonnance viagra naturel cialis 10 mg acquistare levitra procurer du viagra acquisto viagra senza ricetta viagra controindicazioni levitra 20 mg compra viagra impuissance erection acheter cialis pharmacie prezzi levitra viagra ohne rezept kamagra apcalis comprar viagra commander du cialis cialis ricetta medica sildenafil 50 mg sildenafil venta viagra italia pilule levitra sildenafil generico viagra prijs
StatenUtil: C# String-Regex Extenstions and String Scanner
Apr 3rd, 2009 by Jason

Face it, C# is not as regex-friendly as it could be.  To do things like find a single regex match involves several lines or some nasty inline code. Using Extension methods, this can be simplified. In another addition to StatenUtil focused on regex, C# becomes a more coder-friendly language.

StringRegex.cs

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace StatenUtil.RegexExtensions
{
  public static class StringRegex
  {
    public static string RegexReplace(this string source, string regex, string replacement)
    {
      Regex r = new Regex(regex);
      return r.Replace(source, replacement);
    }
    public static string Match(this string source, string regex)
    {
      Regex r = new Regex(regex);
      Match m = r.Match(source);
      return m.Value;
    }
    public static string[] Matches(this string source, string regex)
    {
      Regex r = new Regex(regex);
      MatchCollection mc = r.Matches(source);
      List<string> matches = new List<string>();
      foreach(object o in mc)
        matches.Add(((Match)o).Value);
      return matches.ToArray();
    }
    public static string RegexRemove(this string source, string regex)
    {
      Regex r = new Regex(regex);
      return r.Replace(source, "");
    }
  }
}

This class offers four extension methods to the string class: Replace, Remove, Match first, and Match all. Aside from RegexReplace, the parameters often call for a single regular expression string.

Another addition that C# could use is a string scanner. A string scanner moves through a string one regex at a time matching the beginning of a string. This is useful for parsing languages in the way similar to JSON Parsing in Ruby.

StringScanner.cs

using System;

namespace StatenUtil.RegexExtensions
{
  public class StringScanner
  {
    public string Remaining { get; set; }
    public string Match { get; set; }
    public StringScanner(string input)
    {
      Remaining = input;
    }
    public string Scan(string regex)
    {
      Match = Remaining.Match("^"+regex);
      if (Match != null)
        Remaining = Remaining.Substring(Match.Length);
      return Match;
    }
  }
}

The StringScanner class constructor takes an input string that it will scan. Having only a Scan method, the class has the single use of moving through the input string as regex matches are made. As Scan finds a match, it returns the matching string. Also, the class has two properties: Remaining and Match. Remaining provides what is left in the string, useful for checking if the end of the input string has been met. Match provides the most recent match of the Scan method.

JSON Parsing in Ruby
Mar 18th, 2009 by Jason

Yesterday, I spent awhile working on a RubyQuiz Challenge involving the parsing of JSON. Although I did create it with minimal viewing of the solution, I cannot take credit for the code or the idea.  I did notice that on the site there was no actual merging of their snippets, so I felt inclined to share.

jsonParser.rb

require "strscan"
class JSONParser
	AST = Struct.new(:value)
	def parse(input)
		@input = StringScanner.new(input)
		parse_value.value
	ensure
		@input.eos? or error("Unexpected data")
	end

	private

	def parse_value
		trim_space
		parse_object or
		parse_array or
		parse_string or
		parse_number or
		parse_keyword or
		error("Invalid Data")
	ensure
		trim_space
	end

	#Parses colon separated object hashes
	def parse_object
		if @input.scan(/\s*\{/)
			obj = Hash.new
			more_parts = false
			while key = parse_string
				@input.scan(/\s*:\s*/) or error("Expected : separator")
				obj[key.value] = parse_value.value
				more_parts = @input.scan(/\s*,\s*/) or break
			end
			error("Missing object pair") if more_parts
			@input.scan(/\s*}/) or error("Unclosed object")
			AST.new(obj)
		else
			false
		end
	end

	def parse_array
		if @input.scan(/\s*\[/) #Arrays start with [
			a = Array.new
			more_items = false
			while current = parse_value
			a << current.value
			more_items = @input.scan(/\s*,\s*/) or break
			end
			error("Missing value") if more_items
			@input.scan(/s*\]/) or error("Unclosed array")
			AST.new(a)
		end
	end

	#Parses string objects
	def parse_string
		if @input.scan(/"/)
			s = String.new
			while current = parse_string_content || parse_string_escape
				s << current.value
			end
			@input.scan(/"/) or error('Unclosed String')
			AST.new(s)
		else
			false
		end
	end

	def parse_string_content
		@input.scan(/[^\\"]+/) and AST.new(@input.matched)
	end

	def parse_string_escape
		if @input.scan(%r{\\["\\/]}) #slashes and quotations
			AST.new(@input.matched[-1])
		elsif @input.scan(/\\[bfnrt]/) #newlines, tabs, etc
			AST.new(eval(%Q{"#{@input.matched}"}))
		elsif @input.scan(/\\u[0-9a-fA-F]{4}/) #Hex integers
			AST.new([Integer("0x#{@input.matched[2..-1]}")].pack("U"))
		else
			false
		end
	end

	def parse_number
		@input.scan(/-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?\b/) and
		AST.new(eval(@input.matched))
	end

	def parse_keyword
		@input.scan(/\b(?:true|false|null)\b/) and
		AST.new(eval(@input.matched.sub("null","nil")))
	end

	def trim_space
		@input.scan(/\s+/)
	end

	def error(message)
			raise "#{message}:  #{@input.peek(@input.string.length)}"
	end
end

For an in depth explanation of the individual portions in this code, refer to the RubyQuiz Site . One way that I’ve tested this code is by accessing the Twitter search api. The following snippet will print the 20 most recent #neumont tweets:

require 'jsonParser'
require 'cgi'
require 'open-uri'
url = "http://search.twitter.com/search.json?q=%23neumont"
parser = JSONParser.new
open(url) {|html| @output = html.read}
obj = parser.parse(@output)
obj["results"].each {|e| puts CGI.unescapeHTML(e["text"])}
»  Substance: WordPress   »  Style: Ahren Ahimsa Blog Flux Local - Utah
© Jason Staten 2009