How to parse a time into a Date object from user input in JavaScript?

A quick solution which works on the input that you’ve specified: function parseTime( t ) { var d = new Date(); var time = t.match( /(\d+)(?::(\d\d))?\s*(p?)/ ); d.setHours( parseInt( time[1]) + (time[3] ? 12 : 0) ); d.setMinutes( parseInt( time[2]) || 0 ); return d; } var tests = [ ‘1:00 pm’,’1:00 p.m.’,’1:00 p’,’1:00pm’,’1:00p.m.’,’1:00p’,’1 pm’, … Read more

JavaScriptSerializer.Deserialize – how to change field names

I took another try at it, using the DataContractJsonSerializer class. This solves it: The code looks like this: using System.Runtime.Serialization; [DataContract] public class DataObject { [DataMember(Name = “user_id”)] public int UserId { get; set; } [DataMember(Name = “detail_level”)] public string DetailLevel { get; set; } } And the test is: using System.Runtime.Serialization.Json; [TestMethod] public void … Read more

Boolean expression (grammar) parser in c++

Here is an implementation based on Boost Spirit. Because Boost Spirit generates recursive descent parsers based on expression templates, honouring the ‘idiosyncratic’ (sic) precedence rules (as mentioned by others) is quite tedious. Therefore the grammar lacks a certain elegance. Abstract Data Type I defined a tree data structure using Boost Variant’s recursive variant support, note … Read more

In Java, how do I parse XML as a String instead of a file?

I have this function in my code base, this should work for you. public static Document loadXMLFromString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is); } also see this similar question

Parsing Huge XML Files in PHP

There are only two php APIs that are really suited for processing large files. The first is the old expat api, and the second is the newer XMLreader functions. These apis read continuous streams rather than loading the entire tree into memory (which is what simplexml and DOM does). For an example, you might want … Read more

Parse v. TryParse

Parse throws an exception if it cannot parse the value, whereas TryParse returns a bool indicating whether it succeeded. TryParse does not just try/catch internally – the whole point of it is that it is implemented without exceptions so that it is fast. In fact the way it is most likely implemented is that internally … Read more

What is the difference between an Abstract Syntax Tree and a Concrete Syntax Tree?

A concrete syntax tree represents the source text exactly in parsed form. In general, it conforms to the context-free grammar defining the source language. However, the concrete grammar and tree have a lot of things that are necessary to make source text unambiguously parseable, but do not contribute to actual meaning. For example, to implement … Read more