by mekster · 3y ago · 0 comments
It seems to only exist in log parsing ecosystems but this really helps with getting rid of little bugs and wrong parsing of specific regex patterns.
Instead of doing "^\d+(\.\d+){3}$" for IP checking which is clearly wrong, you'd do "%{IPV4:ip}" which is so much better.
List of known patterns : https://github.com/hpcugent/logstash-patterns/blob/master/fi...
Even for PHP a third party library only has 15 stars.
It's not a bad idea, though you'd want to make it a more formal standard; since it's just used internally in one project, it may be subject to change based on that project's own needs. There could also be more documentation, showing examples of strings that are accepted and not accepted by each pattern, as well as advice for generating compliant strings.
If I came across this in code, I still have similar issues to regex. I have to question the source and what unit testing exists, that the tests cover the particular cases we care about, and if composed it needs tests anyway.
For context, I always split my regexs to a function and add unit tests for them, and ask the same in PRs. It is one of the easiest tests to write and most beneficial: if I don't discover a case I missed while I come up with example inputs, it protects any future modifications by future devs. It also empowers devs that don't know regexs well to be able to modify them (because they know it won't break) and to approve a PR with one.
I don't see how grok patterns avoid the need for tests (especially if composed), and if there are tests anyway, they're just a level of indirection.
In the worst case, when there's inevitably a bug (or app-specific missing situation) in one of these, it's harder to fix than just having a regex directly.
[1] https://github.com/mmkjony/awesome-regex-1 , https://regexlib.com/DisplayPatterns.aspx?cattabindex=5&cate...
In your case, putting the grok patterns in your regex specific function seems to make it more readable.
A common example is email address validation. Trying to validate that an email address is well-formed with regex will often miss valid addresses (the example you linked would miss some in the HOSTNAME portion alone). It would be better to simply send a confirmation link to the address and wait for it to be clicked since that would ensure that the address is valid
I didn't think some milliseconds is worth more than correctness.
Grok pattern is like commenting on your regexp, except you'll also get logical validation instead of bunch of characters you may make a mistake one day.
Further, what happens if the grok pattern is wrong? Or if it changes?
> A specialized regular expression object that matches natural language text for predefined data patterns.
It's targeted at the kind of information end users are interested in: dates, email addresses, like that.
[0]:https://developer.apple.com/documentation/foundation/nsdatad...
It's for parsing rather than search though, you'd still need regex if e.g. searching unstructured logs for IPs.
e.g. (powershell syntax for easy reproduction) returns parsed, or throw if invalid. [uri]"\\ServerName1\Folder\UserName" [version]'1.2.3.4' [ipaddress]'1.1.1.1'
if (int.TryParse(userInput, out var number))
{
total += number;
}
This is cleaner code compared to try/catch and usually faster (though debatable if it matters) [2].[1] https://learn.microsoft.com/en-us/dotnet/api/system.net.ipad...