4 ms·
I find this: mail = '(?P<name>[a-z][a-z0-1\_\.\-]{1,})' hostname = '(?P<hostname>python[a-z]{1,})' domain = '(?P<domain>br[a-z])' match_mail =
by nemetroid 9y ago
I find this:
mail = '(?P<name>[a-z][a-z0-1\_\.\-]{1,})'
hostname = '(?P<hostname>python[a-z]{1,})'
domain = '(?P<domain>br[a-z])'
match_mail = f'{mail}\@{hostname}\.{domain}'
quite more readable and easy to verify than the 50-line example in the link. The real advantage of parser combinators over regexes is the ability to parse data structures instead of just capturing regex groups.
- klenwell 9y agoOr even just code comments, as with this example[0]: import java.util.regex.Pattern; public class Main { public static void main(String[] args) { String regexStr = ""; regexStr += "\\b"; //Begin match at the word boundary(whitespace boundary) regexStr += "\\d{3}"; //Match three digits regexStr += "[-.]?"; //Optional - Match dash or dot regexStr += "\\d{3}"; //Match three digits regexStr += "[-.]?"; //Optional - Match dash or dot regexStr += "\\d{4}"; //Match four digits regexStr += "\\b"; //End match at the word boundary(whitespace boundary) if (args[0].matches(regexStr)) { System.out.println("Match!"); } else { System.out.println("No match."); } } } [0] https://codereview.stackexchange.com/questions/47432/commenting-string-matching-regex https://codereview.stackexchange.com/questions/47432/comment...
- kqr 9y agoPython even supports this in its regex library, where it's called "verbose regexes". You'd write your code as def main(args): regexStr = \ """ \\b # begin match at word boundary \\d{3} # match three digits [-.]? # optional – match dash or dot """ if args[0].match(regexStr): print("match") else: print("no match") It's even better with nested structures, because it allows indentation as well!
- blibble 9y agothose comments are no better than: i++; /* increase i by one */ sure, break up a larger regex to reflect its structure and comment those sections, but something like \\d{3} or [-.]? should really not need commenting
- bpicolo 9y agoRegex has enough sigil spam / characters that mean alternate things than "this is a letter" that mentally parsing it beyond the trivial case is difficult. They're ubiquitous, but they sure aren't a perfect UI
- iogf 9y agoThis is the official regex to check for mail validity. \A(?:[a-z0-9!#$%&'+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'+/=?^_`{|}~-]+)* | "(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f] | \\[\x01-\x09\x0b\x0c\x0e-\x7f])") @ (?:(?:[a-z0-9](?:[a-z0-9-][a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-][a-z0-9])? | \[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3} (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-][a-z0-9]: (?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f] | \\[\x01-\x09\x0b\x0c\x0e-\x7f])+) \])\z according to http://www.regular-expressions.info/email.html http://www.regular-expressions.info/email.html I honestly think that one would be more readable and comprehensive in crocs's syntax. It is not about being only readable it is about a way of reasoning to come up with your regex, crocs permit you to better reason than using regex's syntax. When one uses regex's syntax to reason it is sort of using a low level programming lang to reason, when things get complex it doesnt really pay off the shortness of regex's.
- nemetroid 9y agoIt would definitely be interesting to see it implemented in crocs! However, it's not really fair to suggest that regexes by necessity are monolithic and non-modular. You could construct that... thing... step by step. And likewise, writing the entire thing as a single crocs statement would be at least as unreadable. But my main point was that since crocs compiles down to regex, it doesn't let you do more than regex already does, and it inherits most of regexes' weaknesses. It does afford some level of type safety though, which could be argued makes up for the verbosity (and whether that verbosity is good or bad is probably a religious war).
- iogf 9y agoYes, there are interesting benefits about type safety consequently it improves debugging as well.
- iogf 9y agoThe main benefit about crocs it is purely epistemological in the sense it eases learning and consequently improves reasoning when implementing filters for content instead of using regex approach one would use crocs approach and get benefits from it permiting you to better reason. You can solve a lot of problems using euclidian geometry that you could solve using cartesian/analytic geometry it doesnt mean that you should use just one of them at all. The two permit you to abstract systems and predict behaviors.