Pattern Matching Without Regex – Introducing the Rosie Pattern Language
spin.atomicobject.com
spin.atomicobject.com
I can do this very easily on the command line or in a .<shell>rc file:
ipv4component='[0-9]{1,3}'
ipv4="$ipv4component"'\.'"$ipv4component"'\.'"$ipv4component"'\.'"$ipv4component"
$ grep -E $ipv4 ...
I am bad at regex, so I'm sure the example is a poor definition, but the idea is there. I can make variables that hold components of a regex, and since a regex is just a string I can compose these via concatenation.If I did this a lot, I could build a small helper script (or probably just a set of shell functions) to maintain a library file of regex components that I can use in the shell with grep.
Also I think there's something wrong with this blog's formatting, it appears to be replacing underscores with italics even within code samples.
For example, the more errors you parse, then the better error messages and introspection you have (structured data) when you want to validate.
It's a classic trade-off.
You're right though, doesn't make a very good example.
Ideally syntax vs semantics should be decoupled in most parsing (hence the AST)
It would ofc also be more useful in future parsing too (similar structure, but not ipv4), but thats a much more minor and rarer benefit
here's the real deal courtesy of the URI spec:
IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
dec-octet = DIGIT ; 0-9
/ %x31-39 DIGIT ; 10-99
/ "1" 2DIGIT ; 100-199
/ "2" %x30-34 DIGIT ; 200-249
/ "25" %x30-35 ; 250-255For example this grok pattern (taken from [1] )
%{TIMESTAMP_ISO8601:timestamp} \[%{IPV4:ip};%{WORD:environment}\] %{LOGLEVEL:log_level} %{GREEDYDATA:message}
refers to a pattern called TIMESTAMP_ISO8601 and calls it "timestamp" in the resulting output data structure.
In logstash, TIMESTAMP_ISO8601 is predefined in a patterns file, such as [2], which is made of up of a mix of regex and other patterns like YEAR, MONTHNUM, etc.
TIMESTAMP_ISO8601 %{YEAR}-%{MONTHNUM}-%{MONTHDAY}[T ]%{HOUR}:?%{MINUTE}(?::?%{SECOND})?%{ISO8601_TIMEZONE}?
MONTHNUM is a regex (optional 0 followed by 1-9; or 10 through 12):
MONTHNUM (?:0?[1-9]|1[0-2])
I'm not sure what all of Rosie's base patterns are. This appears to be a valid regex though, from the example: [:alpha:]+ (regex character class and "+" meaning 1 or more). It's C instead of java which is useful in more/different places.
[1] https://www.elastic.co/blog/do-you-grok-grok
[2] https://github.com/logstash-plugins/logstash-patterns-core/b...
$ cat /etc/hosts | netgrep
127.0.0.1
255.255.255.255
..
..What separates this from tools like PEG.js[1] or pest[2]?
[1]: https://pegjs.org/ [2]: https://github.com/pest-parser/pest
As of version 5.10, Perl regex engine implements a complete recursive-descent parsing. Allowing things like Regexp::Grammars[0] to exist. Perl also has a nice PEG parser framework called Pegex[1]
-- [0] - https://metacpan.org/pod/Regexp::Grammars [1] - https://metacpan.org/pod/Pegex
I guess pest is comparable then, but wasn't mature when the author started work on Rosie?
(I'd be curious for a proper comparison, but I'm not really knowledgeable in this area -- I had no idea there were so many alternatives to regex: https://en.wikipedia.org/wiki/Comparison_of_parser_generator...)
This is a different language insofar as it describes PEGs, not regexes, which is fundamentally different and more powerful (it can parse more things).
The naming of patterns isn’t unique, since you can just put regexes in variables in every other language too. However, the syntax in Rosie seems nicer, and sharing is easier.
Edit: OK, I was wrong. It is strictly more powerful than regexes, in that it can correctly match nested pairs.
> Rosie has several benefits over traditional regexes, including the ability to parse recursive structures like HTML and JSON
Rosie has several benefits over traditional regexes, including the ability to parse recursive structures like HTML and JSON, to create new patterns by combining other patterns, and to name patterns. You can combine these named patterns into libraries which you can import and use elsewhere.