4 ms·
This project would be better if it wasn't exactly a 1-to-1 mapping from words/methods to regular expressions. For example, the regex "\d+" maps to the code "di
by justin_oaks 6y ago
This project would be better if it wasn't exactly a 1-to-1 mapping from words/methods to regular expressions. For example, the regex "\d+" maps to the code "digits().oneOrMore()". That doesn't read well in English because it's odd to have an adjective after the noun (i.e. we say "red bird" not "bird red").
Also, a serious weakness in regex is they are "write only", or hard to read. That's because they are compact and don't have discernible sections that are then assembled together.
You can do that yourself in Java by assigning chunks of regex to variables and then concatenating them together, but the regex engine doesn't let you do that itself. You can't name sections of the regex or insert comments into it.
The example
^(?:http)(?:s)?(?:\:\/\/)(?:www\.)?(?:[^\ ]*)$
could be better if it could be broken down into named pieces or commented like this:
^
(?:http)(?:s)? # http or https
(?:\:\/\/) # ://
(?:www\.)? # optional www.
(?:[^\ ]*) # rest of URL (no spaces)
$
- dmarlow 6y agoI love your example of how it should be explained. This helps people correlate the verbal aspects to the regex parts they described. This ultimately reinforces and helps people learn regex more deeply.
- throwaway_pdp09 6y agoI thought java regexes had comments? https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Pattern.html#COMMENTS https://docs.oracle.com/en/java/javase/11/docs/api/java.base...
- justin_oaks 6y agoHuh, I didn't know that. I've read through a fair amount of Java code with regexes and never seen anyone use comments. Maybe it's because Java doesn't have proper multi-line string support built into the language. If you don't have multi-line support in the language then you're more likely to put the comments outside the string: String regex= "^" +"(?:http)(?:s)?" // http or https +"(?:\:\/\/)" // :// +"(?:www\.)?" // optional www. +"(?:[^\ ]*)" // rest of URL (no spaces) +"$";
- throwaway_pdp09 6y ago... which has to be a better way of doing it (comments + regexps in digestible chunks) than having a rather wordy library.
- jodrellblank 6y agoIs this any different from the comment style: a = 1 # set variable a to 1 a += 1 # add 1 to a ? Comments should explain the why, not the what. `\d # digit` yes that is what it says, but why a digit? What is your ^, what is (?:) what is $, why is (?:[^\ ]) "rest of URL" [I know the answers to these, the person the comments are intending to help probably doesn't]. Where's the explanation of what it's supposed to be doing with a limited subset of URLs? # match basic URLs such as: # http://example.org # https://www.example.org # Must have no surrounding spaces, no spaces in the text. ^(?:http)(?:s)?(?:\:\/\/)(?:www\.)?(?:[^\ ]*)$ It's much clearer to me from the one-line regex that it matches "http:////////// http://////////" and that's probably an error. I don't know why it's clearer, maybe because I'm used to looking at https://www.example.org/path/ instead of https :// www. example.org/path/
- stickfigure 6y agoMostly we program in languages that read like a kind of shorthand english, and mature programmers are expected to be able to read the text fluently. WHAT comments tend to be superfluous. Nobody sane reads 100+ character regexes fluently, so WHAT comments are totally appropriate. Any time I write a nontrivial regular expression, I always try leave an example of what it would match in a comment. I've thanked last-year me for this more times than I can count.