Regular Expressions cheat sheet
Regular expression is a powerful tool, and it can save lots of lines codes sometimes. What… » DEV Community | dev.to
Regular expression is a powerful tool, and it can save lots of lines codes sometimes. What… » DEV Community | dev.to
To me, regular expressions are often made far more complicated than they need to be. Sure, there… » DEV Community | dev.to
Regex para torpes » blog.adrianistan.eu | blog.adrianistan.eu
A while back I was researching the most efficient way to check if a number is prime. This lead me to find the following piece of code: public static boolean isPrime(int n) { return !new String(new char[n]).matches(”.?|(..+?)\\1+”); } I was intrigued. While this might not be the most efficient way, it’s certainly one of the less obvious ones, so my curiosity kicked in. How on Earth could a match for the .?|(..+?)\1+ regular expression tell that a number is not prime (once it’s converted to its unary representation)? ...
We all know the regular expression character classes, right? There are 12 standard classes: [:alnum:] [:digit:] [:punct:] [:alpha:] [:graph:] [:space:] [:blank:] [:lower:] [:upper:] [:cntrl:] [:print:] [:xdigit:] But have you seen a visual representation of what these classes match? Probably not. Therefore I created a visualization that illustrates which part of the ASCII set each character class matches. » Peteris Krumins | catonmat.net
Historically, regular expressions are one of computer science’s shining examples of how using good theory leads to good programs. They were originally developed by theorists as a simple computational model, but Ken Thompson introduced them to programmers in his implementation of the text editor QED for CTSS. Dennis Ritchie followed suit in his own implementation of QED, for GE-TSS. Thompson and Ritchie would go on to create Unix, and they brought regular expressions with them. By the late 1970s, regular expressions were a key feature of the Unix landscape, in tools such as ed, sed, grep, egrep, awk, and lex. Today, regular expressions have also become a shining example of how ignoring good theory leads to bad programs. The regular expression implementations used by today’s popular tools are significantly slower than the ones used in many of those thirty-year-old Unix tools. This article reviews the good theory: regular expressions, finite automata, and a regular expression search algorithm invented by Ken Thompson in the mid-1960s. It also puts the theory into practice, describing a simple implementation of Thompson’s algorithm. That implementation, less than 400 lines of C, is the one that went head to head with Perl above. It outperforms the more complex real-world implementations used by Perl, Python, PCRE, and others. The article concludes with a discussion of how theory might yet be converted into practice in the real-world implementations. ...