This depends on reflect. What's the speed penalty?
Go-Restructure: Sane regular expressions with struct fields
11–20 of 47 posts
Re: Go-Restructure: Sane regular expressions with struct fields
#12[deleted]
Re: Go-Restructure: Sane regular expressions with struct fields
#13The example that is used to show the case for the library can be written as https://play.golang.org/p/tuhGjqZlu0 . This is simpler than the code with the library.
Re: Go-Restructure: Sane regular expressions with struct fields
#14Here'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
#15Very clean approach. Also does an excellent job of clearly communicating what the regex is doing, which is saying a lot for regex's in general.
Great concept and implementation.
Re: Go-Restructure: Sane regular expressions with struct fields
#16What's wrong with named capture groups ?
Re: Go-Restructure: Sane regular expressions with struct fields
#17What'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.
Re: Go-Restructure: Sane regular expressions with struct fields
#18This depends on reflect. What's the speed penalty?
That's a very good question indeed, a benchmark should be added just for reference.
Re: Go-Restructure: Sane regular expressions with struct fields
#19Does this work with numerical fields as well? I only see strings in the examples.
Re: Go-Restructure: Sane regular expressions with struct fields
#20Very nice solution! I try to avoid using 3rd party libraries but this I would use without hesitation. Some benchmarks would be interesting.
Why do you avoid third-party libraries?