RegExpBuilder – Create regular expressions using chained methods
85 points by jrullmann 10 years ago | 54 comments- draegtun 10 years agoThought this might be of interest; below shows how the examples provided would look in Rebol:
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_languagedigits: 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
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
- _lce0 10 years agohey thanks to share!
TIL
https://en.wikipedia.org/wiki/RebolAlthough 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]
- carlob 10 years agoMathematica also has its own string pattern sytax
http://reference.wolfram.com/language/ref/StringExpression.h...
Something like that would be
orStringExpression[ "$", Repeated[DigitCharacter], ".", DigitCharacter, DigitCharacter ]
orStringExpression[ "$", Repeated[DigitCharacter], ".", Repeated[DigitCharacter, {2}], ]
and the other isStringExpression[ "$", NumberString ]
This can be made more concise since StringExpression has an infix form (~~) and Repeated can sometimes be replaced by postfix ..StringExpression[ Repeated[ StringExpression[ Repeated["p", {1, Infinity}], Repeated["q", {2, Infinity}] ], {2} ] ]
- akater 10 years ago> Repeated can sometimes be replaced by postfix ..
Always, not sometimes. ;-)
- akater 10 years ago
- raiph 10 years agoPerl 6 unifies "regexes" and recursive descent parsing:
Note that these "regexes" are syntax, not strings, checked and converted in to a hybrid DFA/NFA at compile-time.'$10.00' ~~ rx{ \$ \d+ \. \d\d }; my $pat = rx{ \p+ \q**2..Inf }; 'pqqpqq' ~~ rx{ <$pat>**2 }
- _lce0 10 years ago
- tragomaskhalos 10 years agoThere have been many efforts similar to this in many languages, but most of us seem happy to stick to the more succinct canonical form, supplemented via /x # comments when things get too hairy
- marktangotango 10 years agoGenerally, 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".
- otakucode 10 years agoThe worst regexes I've had to write involved parsing the various IMDB data files, which seem to have been formatted specifically to make them as difficult to parse as possible. I hear mediawiki syntax is similarly arcane and evil, but I've never tried to parse it (though last night I started writing some tools to deal with wikipedia dumps so I might end up in that corner). I'd really like to see different approaches to parsing really ugly formats that feature an exception to almost every single pattern you think you've found. I honestly think the regex is easiest...
- DenisM 10 years agoRecursive descend is imperative, while regex is declarative.
Regex may be ugly, but you lose something important when you move from declarative to imperative.
- jerf 10 years ago"Recursive descent" has that name precisely because it is not the only parsing alternative, hence we can not simply call it "parsing".
- raiph 10 years agoPerl 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...
- jerf 10 years ago
- otakucode 10 years ago
- UnoriginalGuy 10 years agoLooks 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, however I wouldn't want to see a large performance loss either).
- UK-AL 10 years agoRegular 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 your taking work away from it. Your building the tree directly here, where as parser would normally build a tree from the string. But since this is integrating into the languages RE library i'm guessing its writing that tree as a string, which is then passed into the regular expression engine, to be turned into a tree again :)
- UnoriginalGuy 10 years agoI 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%.
So high performance is all relative. However RegX isn't something I'd describe that way, even compiled. It is a nice way to write complex string parsing code quickly however.
- jerf 10 years agoIf your regex is replaceable by a simple "find_substring" or equivalent, it's slow.
If your regex is complicated, it will probably beat any naive attempt to write it into conventional string processing, short of reimplementing regexs in the first place. Especially since in many languages, "conventional string processing" may involve the creation of lots of copies and sub-copies.
- d4n3 10 years ago> 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.
- UK-AL 10 years agoA 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.
- jerf 10 years ago
- Blackthorn 10 years agoI wasn't sure to be impressed or horrified when I learned that Perl supported recursive regular expressions.
- UnoriginalGuy 10 years ago
- amageed 10 years agoIf you're interested in something similar for .NET / C#, check out my Regextra library, specifically the Passphrase Regex Builder: https://github.com/amageed/Regextra
As the name suggests though, the focus was on passphrase criteria and it wasn't to produce a DSL for general regex building. The library also supports named templates and a few utility methods.
- Retra 10 years agoThis 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.
- amageed 10 years agoLINQ 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.,
The same thing in query syntax:var results = SomeCollection.Where(c => c.SomeProperty < 10) .Select(c => new { c.SomeProperty, c.OtherProperty });
Then you can iterate over both the same way:var results = from c in SomeCollection where c.SomeProperty < 10 select new { c.SomeProperty, c.OtherProperty };
foreach (var result in results) { Console.WriteLine(result); }
- amageed 10 years ago
- rripken 10 years agoPerformance is unaffected. This provides a fluent and verbose way of building a regular expression. Users of the library then feed the built regular expression into their standard regular expression engine.
- UK-AL 10 years ago
- chris-at 10 years agoThanks, 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 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 ) ) ```
- _lce0 10 years agoactually 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 "%";
- GhotiFish 10 years agoerr... 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.
- GhotiFish 10 years ago
- tlrobinson 10 years agoProperly 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 ) )
- raiph 10 years agocf 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...
- raiph 10 years ago
- whichdan 10 years agoHN doesn't support Markdown. You'll need to prefix each line with >= 2 spaces for it to be treated as code.
- UnoriginalGuy 10 years agoThat really is Hacker News' worst limitation. I understand if they want to limit what formatting is available, but the fact that basic listing is so clunky is annoying.
- UnoriginalGuy 10 years ago
- _lce0 10 years ago
- jluxenberg 10 years agoS-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")))))
- maratd 10 years agoRegular 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.
- andrewflnr 10 years agoNo, 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).
- thwarted 10 years agoThis is usually borne by the string literal being used to express the regular expression literal syntax in many languages. Perl, for example, has a regular expression literal syntax that is part of the language proper (which has the added benefit that non-dynamic regular expressions can be checked for syntax at compile time). Python, in contrast, doesn't have a first-class regular expression literal, but makes it easier to deal with by prefixing the literal with r or R to create a "raw string" (which exists to avoid excessive backslash escaping). Some regular expression engines use % as the meta-character indicator, which is more compatible with C-style "escape sequences" in double-quoted strings).
If you think characters and logic need to be on different quoting levels, you're not taking the right perspective on regular expressions. \d or \w are not an escaped d or w, they are their own atoms (or "the keywords of the language", if you will), distinct from the atoms that match the ASCII characters 0x64 and 0x77. The thing to remember with regular expressions is always the first lesson presented: (non-meta) characters match themselves, the regular expression /a/ matches the letter a. What's implied here, but rarely said, is that that's not really the letter a in there, but rather an expression that matches the letter a—it just so happens to also look like the thing it matches. This distinction is subtle, but important. This can also be made more evident by using the /x modifier if it's available to spread out the individual expressions (put space between the keywords).
The primary difference in regular expression languages is often how "logic", as you call it, is expressed. PCRE considers, for example, [ to be the character for opening a character class and \[ to match the byte 0x5b. Admittedly, this is confusing when switching engines because 1) not every character matches itself (the expression that matches a character and the character it matches are not visually the same) and 2) other RE engines have taken the opposite approach depending on if that engine was meant, by the author, to have more literal atoms or more logic in its most common use (that is, you save typing if you mean to match the byte 0x5b more frequently than if you mean to open a character class).
As for "quoting", you almost NEVER should be using things like PCRE's \Q…\E (or the quotemeta function) unless you're building regular expressions dynamically from user-input. quotemeta and friends are not readability tools, but safety tools.
- thwarted 10 years ago
- Ronsenshi 10 years agoI agree with you. Every now and then I see mentions of "all-new-regex-builder" on HN frontpage. What is up with regex and desire to write wrappers upon wrappers on top of it?
I see regex like that: if you have to use it often enough, better to learn it as it is - will be more helpful in the long run. If you don't use regex too often then just google your question - there's a very high chance that somebody already wrote regex for your or similar problem.
Only tools I ever use are regex testers (like regexr.com) when I need to make sure that pattern works correctly.
- to3m 10 years agoBut alternative syntaxes are regular expressions too.
- davelnewton 10 years agoIt's not a "crutch", it's an "alternative". Couching it in negative terms isn't really fair.
While I prefer writing regexes, a regex DSL isn't fundamentally better or worse, just different. In addition, it allows non-computer people to write, or at least specify, regexes in a way that makes more sense to non-developers.
- skymt 10 years agoAlternate 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.)
- to3m 10 years agoHere'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):
Of course, you can do this with string concatenation, but I think this syntax makes it clearer what's going on.(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))))
- to3m 10 years ago
- coldtea 10 years ago>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.
- andrewflnr 10 years ago
- maratd 10 years ago
- jgalt212 10 years agoDefinitely 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:
- philjohn 10 years agoLove 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!
- philjohn 10 years ago
- dkarapetyan 10 years agoGeneralize just a little bit and you got parser combinators.
- zzzcpan 10 years agoRegexpes exist to avoid cumbersome code like this, to make it less error prone. Makes me sad to see so many upvotes.
I get that some people have a hard time understanding regexpes with all the backtracking and greediness. Yes, syntax is a bit complicated. Maybe simplified predictable default mode could help. But there is no problem with DSL being used as an abstraction. In fact, we need more DSLs, for everything!
- psychometry 10 years agoNow you have three problems.
- kazinator 10 years agoYes, regexes can have other syntactic representations, like:
Run:(compound "$" (1+ :digit) "." :digit :digit)
$ txr -p "(regex-compile '(compound \"$\" (1+ :digit) \".\" :digit :digit))" #/$\d+\.\d\d/
- epicureanideal 10 years agoNice work! I don't know if it'll be ideal for all use cases, but it does add some readability.
- otakucode 10 years agoNow do an example where you create a regex to parse the IMDB movies.list data file!
- gcao 10 years agoGreat work! This is very intriguing!
- pg_is_a_butt 10 years agoyou know what else can represent all regular expressions? regular expressions.
#dumb