Live data from Hacker News

RegExpBuilder – Create regular expressions using chained methods

github.com

51–56 of 56 posts

Re: RegExpBuilder – Create regular expressions using chained methods

#51
post #13

Generally, I find that if one's regexes are so complex that one needs visualizers or other aids in writing them, one doesn't have a regex problem, but a parsing problem. The method of parsing by recursive descent can often lead to much more understandable (if more verbose) "pattern matching".

Recursive descend is imperative, while regex is declarative. Regex may be ugly, but you lose something important when you move from declarative to imperative.

Perl 6 unifies "regexes" and recursive descent. See https://news.ycombinator.com/item?id=9039680 or, say, https://github.com/Mouq/json5/blob/master/lib/JSON5/Tiny/Gra...

Re: RegExpBuilder – Create regular expressions using chained methods

#52
post #19

Earlier quoted context omitted.

I guess it depends on your definition of "high performance." If a regular expression runs too often, even pre-compiled (as they should be), you'll want to replace them with code written in the native language. I've gone in and replaced a one line search/replace written in RegX (compiled), with just a C-style for() loop over the wchar array, and had the memory usage drop by near 80% and performance increase by over 60…

A regular expression implemented as a DFA would literally be looping over the string, and a state transition table. I don't see how performance could be bad. It is highly dependent on the regular expression engine you use, most don't use automata because of extra features.

Perl 6 unifies "regexes" and recursive descent grammars at the syntax level and then compiles them to a hybrid of DFAs, NFAs, and regular code as necessary. The idea is to maintain the simplicity of simple regexes and of parser combinators for the user but retain as much of the performance benefits of true DFA-able regular expressions as is possible.

At least, that's the theory. In practice, while the benefit of syntactic usability is available today, the Perl 6 rules engine is still very slow and it'll likely take years to optimize the heck out of this approach and really harvest the performance benefit.

Re: RegExpBuilder – Create regular expressions using chained methods

#53
post #2

Thanks, this is a lot better than writing this (even if the formatting worked here): ``` (?xi) \b ( # Capture 1: entire matched URL (?: [a-z][\w-]+: # URL protocol and colon (?: /{1,3} # 1-3 slashes | # or [a-z0-9%] # Single letter or digit or '%' # (Trying not to match e.g. "URI::Escape") ) | # or www\d{0,3}[.] # "www.", "www1.", "www2." … "www999." | # or [a-z0-9.\-]+[.][a-z]{2,4}/ # looks like domain name followed…

Properly formatted (to be fair this is from a blog post explaining how the regex works: http://daringfireball.net/2010/07/improved_regex_for_matchin... ): (?xi) \b ( # Capture 1: entire matched URL (?: [a-z][\w-]+: # URL protocol and colon (?: /{1,3} # 1-3 slashes | # or [a-z0-9%] # Single letter or digit or '%' # (Trying not to match e.g. "URI::Escape") ) | # or www\d{0,3}[.] # "www.", "www1.", "www2." … "www999." |…

cf the Perl 6 community module for parsing URIs which features Perl 6's unique unification of regexes and grammars:

https://github.com/perl6-community-modules/uri/blob/master/l...

Re: RegExpBuilder – Create regular expressions using chained methods

#54
post #39

Looks like Linq (from .Net/C#). Pretty sexy way to write Regular Expressions if you ask me. I've "learned" regular expressions multiple times but it just never sticks, I have no idea why. It certainly doesn't help that there are several different incompatible syntaxes (so what I remember and think "should" work doesn't). I'd prefer to write RegX's in this style, however I would pay attention to performance (not that…

This is why I dislike the design of Linq. The pattern of chaining function calls to implement a DSL is common enough that they should have employed a general solution, not just a wonky SQL-specific version.

LINQ isn't SQL-specific and does apply generally. It can be used against the standard .NET framework objects and collections. There are different LINQ focuses or flavors, and there are 2 ways to write queries. There's LINQ to Objects, LINQ to XML, LINQ to SQL (no longer actively maintained; nowadays Entity Framework is the Microsoft alternative), and you can write your own LINQ providers to target other purposes.

As for syntax, there's the fluent syntax (chained methods), and there's the query syntax which is syntactic sugar that gets compiled to the methods. The query syntax is probably the biggest reason people mistake LINQ for being SQL specific since it resembles SQL.

E.g.,

  var results = SomeCollection.Where(c => c.SomeProperty  new { c.SomeProperty, c.OtherProperty });
The same thing in query syntax:

  var results = from c in SomeCollection
                where c.SomeProperty 
Then you can iterate over both the same way:

  foreach (var result in results)
  {
      Console.WriteLine(result);
  }

Re: RegExpBuilder – Create regular expressions using chained methods

#55
post #43

Thought this might be of interest; below shows how the examples provided would look in Rebol: digits: digit: charset "0123456789" rule: [ thru "$" some digits "." digit digit ] parse "$10.00" rule ;; true pattern: [ some "p" 2 "q" any "q" ] new-rule: [ 2 pattern ] parse "pqqpqq" new-rule ;; true Rebol doesn't have regular expressions instead it comes with a parse dialect which is a TDPL - http://en.wikipedia.org/wiki…

Mathematica also has its own string pattern sytax http://reference.wolfram.com/language/ref/StringExpression.h... Something like that would be StringExpression[ "$", Repeated[DigitCharacter], ".", DigitCharacter, DigitCharacter ] or StringExpression[ "$", Repeated[DigitCharacter], ".", Repeated[DigitCharacter, {2}], ] or StringExpression[ "$", NumberString ] and the other is StringExpression[ Repeated[ StringExpressi…

> Repeated can sometimes be replaced by postfix ..

Always, not sometimes. ;-)

Re: RegExpBuilder – Create regular expressions using chained methods

#56

Earlier quoted context omitted.

Love it - just visualised the PCRE generated from the EBNF for the N-Triples RDF serialisation format[1] :) https://www.debuggex.com/r/Yxqws81Uif-BGBN8 Important note - this is built up programmatically, it's not just a string dumped in a parser! [1] http://www.w3.org/TR/n-triples/#n-triples-grammar

That is one hairy regex. Now the inverse would be even better. You modify the railroad chart and the regex updates.

Fairly hairy, yes, but if you follow the railroad tracks, it's quite succint for what it's doing!
Post reply on HN