Live data from Hacker News

RegExpBuilder – Create regular expressions using chained methods

github.com

31–40 of 56 posts

Re: RegExpBuilder – Create regular expressions using chained methods

#31

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…

hey thanks to share!

TIL

    Although Rebol can be used for programming, 
    writing functions, and performing processes, 
    its greatest strength is the ability to 
    easily create domain-specific languages or 
    dialects.
        — Carl Sassenrath [Rebol author]
https://en.wikipedia.org/wiki/Rebol

Re: RegExpBuilder – Create regular expressions using chained methods

#32
post #27

S-expressions are a natural fit for construction of regular expressions, see http://community.schemewiki.org/?scheme-faq-programming#H-1w... e.g. (: (or (in ("az")) (in ("AZ"))) (* (uncase (in ("az09")))))

Regular expressions are a natural fit for construction of regular expressions. Look, I know it takes a while, but once you get the hang of it, you won't need any crutches to write regular expressions. The only tool that's really needed is a way to rigorously test a regular expression to make sure it does what it needs to do and there are a ton of those around.

No, they're really not, as evidenced by all the quoting and meta-character nonsense you have to deal with. Sure, it's not too difficult to figure out, most of the time, but I think a solution that puts characters and logic on different quoting levels will almost always be better from an expressiveness standpoint (ignoring ecosystem issues).

Re: RegExpBuilder – Create regular expressions using chained methods

#33
post #28

Earlier quoted context omitted.

> you'll want to replace them with code written in the native language Probably not true for Javascript (and other scripted languages) - matching regex uses native and highly optimized regex lib, which will usually be orders of magnitude faster than implementing this in the language.

That isn't relevant in this context as the library linked couldn't be integrated into JavaScript.

Sorry, which library do you mean? The OP is a javascript library..

I just wanted to point out that regex is much faster in javascript than doing things 'by hand'.

Re: RegExpBuilder – Create regular expressions using chained methods

#35

Definitely a debugable way to write regexes. Whenever I have to maintain a hairy regex, I like to plot the regex as a railroad diagram. These web based tools can do it: https://www.debuggex.com/ http://jex.im/regulex/

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

Re: RegExpBuilder – Create regular expressions using chained methods

#36
post #27

S-expressions are a natural fit for construction of regular expressions, see http://community.schemewiki.org/?scheme-faq-programming#H-1w... e.g. (: (or (in ("az")) (in ("AZ"))) (* (uncase (in ("az09")))))

Regular expressions are a natural fit for construction of regular expressions. Look, I know it takes a while, but once you get the hang of it, you won't need any crutches to write regular expressions. The only tool that's really needed is a way to rigorously test a regular expression to make sure it does what it needs to do and there are a ton of those around.

But alternative syntaxes are regular expressions too.

Re: RegExpBuilder – Create regular expressions using chained methods

#37
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."
        |                           #   or
        [a-z0-9.\-]+[.][a-z]{2,4}/  # looks like domain name followed by a slash
      )
      (?:                           # One or more:
        [^\s()]+                      # Run of non-space, non-()
        |                               #   or
        \(([^\s()]+|(\([^\s()]+\)))*\)  # balanced parens, up to 2 levels
      )+
      (?:                           # End with:
        \(([^\s()]+|(\([^\s()]+\)))*\)  # balanced parens, up to 2 levels
        |                                   #   or
        [^\s`!()\[\]{};:'".,?«»“”‘’]        # not a space or one of these punct chars
      )
    )

Re: RegExpBuilder – Create regular expressions using chained methods

#38
post #29
post #27

Earlier quoted context omitted.

Regular expressions are a natural fit for construction of regular expressions. Look, I know it takes a while, but once you get the hang of it, you won't need any crutches to write regular expressions. The only tool that's really needed is a way to rigorously test a regular expression to make sure it does what it needs to do and there are a ton of those around.

Alternate representations of regexes aren't necessarily a crutch to avoid learning the normal syntax. S-expressions in particular could be useful for runtime manipulation or generation of patterns without the bother of string mangling. (I can't think of a reason to do so off-hand, but it's a nifty capability.)

Here's an example of this kind of thing from some emacs lisp I wrote (which I hope survived the transition to the HN comment box):

    (setq imenu-generic-expression
      (let ((ident '(1+ (any "A-Za-z0-9_"))))
        `(("plugin" ,(rx line-start
                         (0+ space) "plugin"
                         (1+ space) (eval ident)
                         (1+ space) (group (eval ident)))
                         1))))
Of course, you can do this with string concatenation, but I think this syntax makes it clearer what's going on.

Re: RegExpBuilder – Create regular expressions using chained methods

#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.

Re: RegExpBuilder – Create regular expressions using chained methods

#40
post #27

S-expressions are a natural fit for construction of regular expressions, see http://community.schemewiki.org/?scheme-faq-programming#H-1w... e.g. (: (or (in ("az")) (in ("AZ"))) (* (uncase (in ("az09")))))

Regular expressions are a natural fit for construction of regular expressions. Look, I know it takes a while, but once you get the hang of it, you won't need any crutches to write regular expressions. The only tool that's really needed is a way to rigorously test a regular expression to make sure it does what it needs to do and there are a ton of those around.

>Regular expressions are a natural fit for construction of regular expressions.

The particular syntax we use (which is not that great) is not THE "regular expressions" is just one syntax we arrived at.

That is, the "regular expressions" name doesn't refer to the syntax, but to the concept.

Post reply on HN