Live data from Hacker News

Go-Restructure: Sane regular expressions with struct fields

github.com

11–20 of 47 posts

Re: Go-Restructure: Sane regular expressions with struct fields

#14
Here's a 1-1 port in JavaScript https://github.com/benjamingr/js-restructure

    function matcher(obj) { "use strict";
      let props = Object.getOwnPropertyNames(obj);
      const re = new RegExp(props.reduce((p, c) => p + (c.startsWith("_") ? obj[c] : `(${obj[c]})`), ""));
      props = props.filter(x => !x.startsWith("_"));
      return function(pattern) { 
      let o = {};
      const res = re.exec(pattern);
      for(let i = 0; i 
And example usage:

```js matcher({ _ : "^", user : "\\w+", _2 : "@", host : "[^@]+", _3 : "$" })("user@ycombinator.com") ```

Re: Go-Restructure: Sane regular expressions with struct fields

#17
post #16

What's wrong with named capture groups ? http://www.regular-expressions.info/named.html

The support for named capture groups in Go's regular expressions is severely lacking, in the sense that it may as well not even exist. There's an API (SubexpNames) that returns an ordered list of the capture group names (if you provided them), but it's up to the user to implement some scheme for mapping a name to the contents of a capture group. At that point, it's probably going to be easier to just use the capture group indices.
Post reply on HN