Hacker News new | ask | show | jobs
by avgcorrection 1678 days ago
I respect regex for being a widespread DSL that makes certain things very declarative.

My problem with it (other than all the different flavors—that’s just history’s fault) is that you don’t tend to get integrated help/tool support for them. They are often just plain strings—how is e.g. Intellij supposed to spot them reliably and help you?

It’s too bad if we have to copy and paste regex strings into websites in order to figure out what they do.

The number one most useful tool support in my book would just be to highlight metacharacters. Are parens a metacharacter in the dialect that I am using now? Is `+` significant?

A bigger ask would be more verbose regular expression declarations and compilers that can translate to and from them. It’s nice if you can use comments in a regex dialect, but comments should be treated like they are treated in code writ large; don’t use them if you can make the code “self-describing”. Imagine a more verbose declaration language where you can make local aliases, for example `let ident = [a-z][a-z1-9_]*`.

4 comments

I agree that it would be nice to get native syntax highlighting for strings, especially for languages where regular expressions are not a first class feature

However, I would realllyyy not want to have a more verbose regex dialect. For me at least, it would make recognizing common regex patterns a lot harder since they won't be succinct. It would also be yet _another_ regex variety that I would have to remember.

For me, Perl's DEFINE feature and 'x' switch are definitely close enough[1] to variables and comments. Pardon, I don't remember what they are officially called. What we really need is for more non PCRE engines to implement these features

[1]: https://regex101.com/r/z5pGvZ/6

> They are often just plain strings—how is e.g. Intellij supposed to spot them reliably and help you?

WebStorm or any IDE that has WebStorm functionality built-in can detect `/pattern/` in Javascript and give you an option to test it and warn you of syntax errors. PyCharm will help you with functions in `re` module, Rider with `Regex.IsMatch` and so on, all JetBrains IDEs have some sort of contextual help that gives you guidance on stdlib functions that accept regular expressions.

https://www.jetbrains.com/webstorm/guide/tips/check-regex/

Julia has a "readable regex" library that uses descriptive English names. It's a lot like a SQL query builder, but it builds regex.

https://discourse.julialang.org/t/ann-readableregex-jl/43450

https://github.com/jkrumbiegel/ReadableRegex.jl

> how is e.g. Intellij supposed to spot them reliably and help you

It depends on the language. JavaScript has a dedicated RegExp object. If you use the literal notation or constructor, you will always get IntelliJ support (unless you decide to use the constructor and extract the regex string for whatever reason). I _think_ you could also mark a standalone string variable with JSDoc to get IDE support, too.