Live data from Hacker News

RegExpBuilder – Create regular expressions using chained methods

github.com

21–30 of 56 posts

Re: RegExpBuilder – Create regular expressions using chained methods

#21
post #12

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…

Regular expressions are high performance if you use automata style(Regular Language) regular expressions, which limits the use of some of the features you can use. Modern regular expression engines in a lot of languages, actually go beyond the expressiveness of a regular language. This is what damages performance. There is no reason why this would reduce performance... if its not doing anything crazy. If anything you…

I wasn't sure to be impressed or horrified when I learned that Perl supported recursive regular expressions.

Re: RegExpBuilder – Create regular expressions using chained methods

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

"Recursive descent" has that name precisely because it is not the only parsing alternative, hence we can not simply call it "parsing".

Re: RegExpBuilder – Create regular expressions using chained methods

#24
post #14
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…

actually most of the comments seem to imply that whoever wrote that don't fully understand regexp syntax -- or, worst, she expects that whoever read will not /{1,3} # 1-3 slashes | # or [a-z0-9%] # Single letter or digit or "%";

err... sorry?

https://www.debuggex.com/r/EpocMU_7Fq_B_p9z

edit:

wait, I thought about it for a second and I see what you meant. You're not saying it's wrong, you're saying it's obvious.

I wasn't sure if it was obvious because I wasn't sure if {1,3} was supposed to be {1-3} and there was a mistake in the expression, or if there was some kind of unexpected error in the [a-z0-9%] expression.

Because even in this simple example, there is room for error.

Re: RegExpBuilder – Create regular expressions using chained methods

#26
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/Top-down_parsing_language

Some parse refs: http://en.wikibooks.org/wiki/REBOL_Programming/Language_Feat... | http://www.rebol.net/wiki/Parse_Project | http://www.rebol.com/r3/docs/concepts/parsing-summary.html

Re: RegExpBuilder – Create regular expressions using chained methods

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

Re: RegExpBuilder – Create regular expressions using chained methods

#28
post #12

Earlier quoted context omitted.

Regular expressions are high performance if you use automata style(Regular Language) regular expressions, which limits the use of some of the features you can use. Modern regular expression engines in a lot of languages, actually go beyond the expressiveness of a regular language. This is what damages performance. There is no reason why this would reduce performance... if its not doing anything crazy. If anything you…

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…

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

Re: RegExpBuilder – Create regular expressions using chained methods

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

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

Re: RegExpBuilder – Create regular expressions using chained methods

#30
post #28

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…

> 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.
Post reply on HN