12 ms·
Show HN: Regex Cheatsheet
- geongeorgek 7y agoI used to spend hours trying to craft the perfect expression for my scraping projects not realizing that I don't really know regex. This tool is a cheat sheet that also explains the commonly used expressions so that you understand it. - There is a visual representation of the regular expression (thanks to regexpr) - The application shows matching strings which you can play around - Expressions can be edited and these are instantly validated
- __tk__ 7y agoI'm loving the graphs which for the first time in years are giving me an idea of what an expression is actually doing. Just because the visualization is kept in a form that is easy to understand with a programming background but can also be translated to the expression itself in a straightforward manner.
- geongeorgek 7y agoI can't take credit for the visualizations although implementing it was a pain in the ass. It was originally created by: https://regexper.com/ https://regexper.com/
- noxToken 7y agoGraphs for these really hammer home the point that regular expressions aren't magic. Parsers have so many abilities that when starting out, my expressions were horribly inefficient and missed many corner cases. Learning to graph them just like automata immediately made things easier. When green devs are having trouble with regular expressions (and don't have a formal computer science background), I like to give them a crash course in DFAs.
- leibnitz27 7y agoI knocked up a silly dynamic regex grapher a while back as a little teaching aid - mildly fun https://www.benf.org/other/regexview/ https://www.benf.org/other/regexview/
- StavrosK 7y agoI love regex and have no trouble reading them, but still love this tool, great job. I especially like the railroad diagrams, for those cases where I brainfarted on a regex and it's doing something other than what I intended. Thanks for this.
- geongeorgek 7y agoI'm glad you like the tool <3 It will have a lot more content soon :)
- chirss 7y agoIf you want some help swing by #regex on efnet, happy to help.
- Glench 7y agoPlug for Verbal Expressions (no affiliation), which has an alternate way of compiling more human-readable regexes for a dozen languages: http://verbalexpressions.github.io/ http://verbalexpressions.github.io/
- geongeorgek 7y agoThis looks nice
- certifiedloud 7y agoA CLI version of this would be pretty useful to me.
- linusjs_ 7y agoI remember that library. A year after I made regexpbuilder https://www.npmjs.com/package/regexpbuilder https://www.npmjs.com/package/regexpbuilder that library suddenly appeared, and was basically a rip-off of the concept I appear to have created (there was no such other library before regexpbuilder), but is also fairly useless because it doesn't look like it could represent more than about 10% of the possible regular expressions. Yet there was no mention of my library at all in the readme of verbal expressions.
- mimixco 7y agoThis is awesome! Thank you! I hate regex, too, but I love your inline railroad diagramming tool.
- geongeorgek 7y agoHaha thank you <3
- kitd 7y agoThis is really cool! 2 points: 1. it fiddled with my back button which is a bit annoying 2. a better email sample is ^[^@]+@[^@]+\.[^@]+$ which removes the 2 ampersands problem.
- geongeorgek 7y agoThank you! I think I know what's wrong with your back button. I will fix it. And for the regex. will try it out and see if I can add it.
- skrebbel 7y agoYou'll probably want to add \S to those character classes as well, or it matches "it's an @ sign. not an ampersand."
- anamexis 7y agoEscaped or quoted whitespace is allowed in the local part of email addresses.
- laumars 7y agoEven that is wrong because you can have privately owned TLDs (I forget what they're technically called) like .google So sundar.pichai@google is technically a valid address (whether .google has any MX records is another matter) Regex shouldn't really be used for email addresses anyway because the only reliable way to authenticate an email address is to literally send an email to that address.
- crispyambulance 7y agoI use regex a lot but deliberately keep it simple. One thing that confounded me often was positive and negative look-arounds. I always got the expressions mixed up, until I just put the expressions into a table like this... look-behind | look-ahead ------------------------------------ positive (?<=a)b | a(?=b) ------------------------------------ negative (?<!a)b | a(?!b) It's not hard, but for whatever reason my brain had trouble remembering the usage because every time I looked it up, each of those expressions was nested in a paragraph of explanation, and I could not see the simple intuitive pattern. Putting it into a simple visualization helps a lot. Now, if I can find a similar mnemonic for backreferences !?
- geongeorgek 7y agoThis is really intended for beginners. but I can confirm more content is coming soon <3
- wahern 7y agoMaybe it's easier to remember that lookbehinds are evil from an implementation standpoint, and even in Perl have arbitrary limitations. If you see lookbehinds, look away! If you see lookaheads, go ahead.
- ygra 7y agoIt's something I really like about .NET's regular expressions. Lookbehind has no limitations and will just match backwards with all features you can use in other parts. So depending on the language or flavor you're working in, running away isn't really necessary.
- lonelappde 7y agoLookbehinds stay behind.
- glangdale 7y agoOddly, lookbehinds are evil only in a specific backtracking world. We never got around to implementing arbitrary lookarounds in Hyperscan (https://github.com/intel/hyperscan https://github.com/intel/hyperscan) but if we had done something in the automata world to handle lookaround, lookbehinds are way easier than lookaheads. To handle a lookbehind, you really only need to occasionally 'AND' together some states (not an operation you would normally do in a standard NFA whether Glushkov or Thompson). To handle lookaheads... well, it gets ugly.
- philshem 7y agoI have a secret hobby of answering python + regex questions on stackoverflow with pure python.
- geongeorgek 7y agoI'm gonna pretend I didn't read this
- johnnylambada 7y agoExamples?
- philshem 7y ago_secret_
- Amarok 7y ago^[a-z0-9_-]{3,15}$ The username reference doesn't match 16 characters as claimed
- geongeorgek 7y agoI should match. the number 15 there means that repeat x up to 15 times. so 1+15=16. looks good to me
- asicsp 7y agowhere does the extra 1 come from? a{2,5} means match 'a' two to five times
- aratauto 7y agoThat is not correct. 15 is total maximum number of repeats including the first one. Even the diagram on https://ihateregex.io/expr/username https://ihateregex.io/expr/username correctly says that loop can be taken between 2 and 14 times.
- darau1 7y agoNobody pointed it out, but there's also https://regexr.com/ https://regexr.com/ It's how I learned regex years ago, and I still use it today to test/build more complex patterns.
- jve 7y agoWell, there is a whole list of useful regex links posted 5 months ago when someone posted url to RegExr. Enjoy: https://news.ycombinator.com/item?id=20614847 https://news.ycombinator.com/item?id=20614847
- strig 7y agoMy go-to is https://regex101.com/ https://regex101.com/
- darau1 7y agoDidn't know about this. Thanks!
- chirss 7y agoWe use it on slack and irc for debugging people's regular expressions all the time. Being able to have 30 revisions to a base regex to troubleshoot is fantastic. Plus the quiz is awesome.
- 52-6F-62 7y agoI use the same as a default. It's been a great help.
- bepvte 7y agoI love regex101. It uses webassembly for some of its engines
- huseyinkeles 7y agoI've been using regex101 for many years and love it! The debugger [0] that it has is amazing! [0] - https://regex101.com/debugger https://regex101.com/debugger
- ape4 7y agoThe IPv6 regex is surprisingly complicated.
- geongeorgek 7y agoYeah. this is when you start to have 2 problems
- dan_hawkins 7y agoIs there a bug? In regexp for IPv4: https://ihateregex.io/expr/ip https://ihateregex.io/expr/ip expression ends with {3} but the diagram states "2 times" in lower right - shouldn't it say "3 times"?
- jve 7y agoI think it says "repeat 2" times. So basically you'v already went through the group and then 2 more times. Because if I specify x{0,3}, i have 2 paths - around x and thru x + at most 2 more times
- geongeorgek 7y agoYep you are right
- blauditore 7y agoWould be nice to have a regex for parsing HTML... grabs popcorn
- arkh 7y agoWith subroutines and recursive patterns I think you could do something parsing valid HTML. Your sanity won't be left intact tho.
- asicsp 7y agohow about this "match "A B C" where A+B=C"[1] for sanity? [1] http://www.drregex.com/2018/11/how-to-match-b-c-where-abc-beast-reborn.html http://www.drregex.com/2018/11/how-to-match-b-c-where-abc-be...
- geongeorgek 7y agoHaha..careful. someone might take this seriously
- bmn__ 7y agoEasy with a sufficiently powerful engine: https://stackoverflow.com/a/4234491 https://stackoverflow.com/a/4234491 Relies on ?(DEFINE): http://p3rl.org/perlre#(DEFINE) http://p3rl.org/perlre#(DEFINE)
- quickthrower2 7y agoThere is a good comment on that answer: > To sum up: RegEx's are misnamed. I think it's a shame, but it won't change. Compatible 'RegEx' engines are not allowed to reject non-regular languages. They therefore cannot be implemented correctly with only Finte State Machines. The powerful concepts around computational classes do not apply. Use of RegEx's does not ensure O(n) execution time. The advantages of RegEx's are terse syntax and the implied domain of character recognition. To me, this is a slow moving train wreck, impossible to look away, but with horrible consequences unfolding
- chirss 7y ago
- axegon 7y agoThis is awesome but.... I don't hate regex. Matter of fact, I love regex.
- geongeorgek 7y agocheck out the ipv6 one :)
- axegon 7y agoI've had to write regex for deeply proprietary SQL-like (the word "like" is a big BIG stretch) language. This really is nothing. The regex itself was 4 pages long. AFAIK they still use it in production, almost 10 years later with 0 modifications. ¯\_(ツ)_/¯
- lfglopes 7y agoI used to use this site http://txt2re.com http://txt2re.com which is now off the grid, at the least since yesterday. :( Unlike most regex helpers, in this one you would start with the text you want to filter/parse and then it would suggest you possible extractions. Do you know any alternatives?
- deadliftpro 7y agosame, looking for an alternative to txt2re.
- asicsp 7y agoneat site! clicking an example opens up a playground with live update and explanation and railroad diagrams, similar to sites like regex101[1] and regulex[2] one suggestion would be to mention clearly which tool/language is being used, regex has no unified standard.. based on "Cheatsheet adapted" message at the bottom, I think it is for JavaScript. I wrote a book on js regexp last year, and I have post for cheatsheet too [3] [1] https://regex101.com/ https://regex101.com/ [2] https://jex.im/regulex https://jex.im/regulex [3] https://learnbyexample.github.io/cheatsheet/javascript/javascript-regexp-cheatsheet/ https://learnbyexample.github.io/cheatsheet/javascript/javas...
- geongeorgek 7y agoTotally agreed! Right now I only support javascript. But for everything shown there, it's pretty much the same for most flavors
- deleted 7y ago[deleted]
- KenanSulayman 7y agoI don't understand why the Github repository lists regexper as the source of the visual graph code but the frame only shows iHateRegex as watermark? If the only thing that is embedded in that frame was taken entirely from a different project, that project should at least be mentioned in the frame.
- vzidex 7y agoVery cool! The site that worked best for me to learn regex was https://regexcrossword.com/ https://regexcrossword.com/ - after solving my way through all of them (I got really hooked when I discovered the site) I found I was alright at regex.
- geongeorgek 7y agoThank you for sharing that. looks good
- xxsaculxx 7y agoNice tool! I personally use https://regex101.com/ https://regex101.com/ as I like the explanations and quick reference.
- adambowles 7y ago>/h.llo/ the '.' matches any one character other than a new line character... matches 'hello', 'hallo' but not 'h llo' in the cheatsheet is false. (https://regexr.com/4tc48 https://regexr.com/4tc48) `.` can match any character except linebreaks (including whitespace)
- jodrellblank 7y ago`.` "can" match any character including linebreaks if the regex engine is in re.DOTALL mode (Python) or SingleLine Mode (.Net).
- robert_tweed 7y agoOK, these kinds of regex tools get posted quite often. I get it, regex is very confusing at first. And some of these use-cases result in rather complex expressions nobody should be forced to write from scratch (you are still remembering to write unit tests for them though, right?) But as someone who actually knows [some flavours of] regex fairly well, what I would really like, is a reference that covers all the subtle differences between the various regex engines, along with community-managed documentation (perhaps wiki pages) of which applications & API versions use which flavour of regex. For example, the other day I wanted to run a find on my NAS. I needed to use a regex, but the Busybox version of find doesn't support the iregex option, so all expressions are case-sensitive. With some googling, I was able to find out that the default regex type is Emacs, but I wasn't able to find either a good reference for exactly what Emacs regex does and doesn't support, nor any information about how to set the "i" flag. In the end I had to manually convert every character into a class (like [aA] for "a") which was tedious, but quicker than trying to find a better solution or resorting to grep. A related, annoyingly common pattern is that the documentation for `find` states that `--regex` specifies a regex, but it does not state which flavour of regex. The documentation for certain versions of `find`, which support alternative engines, note that the default is Emacs. From this I was able to infer (perhaps wrongly) that the Busybox `find` uses Emacs-flavoured regex, but ultimate I still had to resort to some trial-and-error. This problem is all too common in API documentation.
- geongeorgek 7y agoYou're totally right. Right now this tool only supports the javascript flavor of regex. That said, for all the simple expressions shown there it's more or less the same for most other engines. I guess that makes it okay.
- celeritascelery 7y agoThe O’Riley book “mastering regular expressions” has a whole section dedicated to it. As well as several tables. But it would be nice to have an online version.
- wyclif 7y ago
- dana321 7y agoOne thing i've always missed from the Perl programming language is the regex operators. You could do: my $var='foo foo bar and more bar foo!!!'; if($var=~/(foo|bar)/g){ # does the variable contain foo or bar? print "foo! $1 removing foo..\n"; # remove our value.. $var=~s/$1//g; }
- olalonde 7y agoThumbs up for the relatable domain name.
- geongeorgek 7y agoGlad you find it that way
- binarysneaker 7y agoThese regexs are garbage. Others have suggested better sites for learning how to construct regexs, and stackoverflow has plenty of great examples.
- geongeorgek 7y agoWhy don't you link them with the comment
- hyperpape 7y agoReally nice idea. I found that you can see your own regex with railroad diagram by going to one of the prepopulated examples and editing it. However, it wasn't clear to me that's the intended use of the tool. It's either a little side-effect, or not super-discoverable.
- superasn 7y agoRegex are quite simple and useful but my only issue is with those recursive things. Like how do you match balanced brackets? I have a regex (pcre) copy-pasted for it but for the life of me I don't get it or maybe nod my head but instantly ununderstand it. I wish there was a simple to understand doc that teaches to me how I can match something like: "(this is inside a bracket (and this is nested or (double nested))) P.S. I know token parsing is better for these things but still I just want to learn the other thing too.
- chirss 7y agoCan you explain the problem further?
- superasn 7y agoplease see my reply to @gizmo686
- chirss 7y agoI guess I don't understand. Mind throwing up an example with multiple test strings on regex101.com ? I'd like to take a look and see if I can make a regex which does what you want. So if you could write the examples there, and then a description like you would tell your mom of what you want I'll see what I can do.
- gizmo686 7y agoBalanced paranthesis are not a regular language, so it s theoretically imposdible to match them with regular expressions. In practice, most regexp implemenations you see are more powerful then regular expressions. For instance, .net has a balancing groups feature [0] for exactly this usecase. [0] https://regular-expressions.mobi/balancing.html?wlr=1 https://regular-expressions.mobi/balancing.html?wlr=1
- superasn 7y agoThe regex I've copy-pasted is this: $str = "(this is inside a bracket (and this is nested or (double nested)))"; do { preg_match_all('~\(((?:[^\(\)]++|(?R))*)\)~', $str, $matches); echo $str = $matches[1][0] ?? '', "\n"; } while($str); Outputs this [1]: > this is inside a bracket (and this is nested or (double nested)) > and this is nested or (double nested) > double nested You're right that there is more processing involved (e.g. while loop) but I still don't understand this part '~\(((?:[^\(\)]++|(?R))*)\)~' [1] https://rextester.com/MEH86820 https://rextester.com/MEH86820
- esaym 7y agoEither I'm a regex wizard and don't know it, or perhaps I think I know something but know nothing at all but I've never complained about using regex expressions. I use them all the time without thought. Never quite figured out the need for a cheatsheet either, your language of choice should have a good documentation page for any specific supported syntax.
- mNovak 7y agoI always refer back to http://rexegg.com/ http://rexegg.com/ Not a tool as such, but a good reference if you know how it works and just need to refresh on syntax.
- rubyn00bie 7y agoNice work on this! Something subtle, but I quite loved the email regex is, IMHO, close to perfect: \S+@\S+\.\S+ Because the "perfect" one is just absurd, and no one realizes it's going to be so fucking absurd until they start getting support cases and then go read something like this: https://stackoverflow.com/a/201378/931209 https://stackoverflow.com/a/201378/931209 > If you want to get fancy and pedantic, implement a complete state engine. A regular expression can only act as a rudimentary filter. The problem with regular expressions is that telling someone that their perfectly valid e-mail address is invalid (a false positive) because your regular expression can't handle it is just rude and impolite from the user's perspective.
- p4lindromica 7y agoEven this regexp has false positives. The `ai` ccTLD ran their own mail server at the root, so an address like `a@ai` was a valid email address. They serve a website at the tld root: http://ai./ http://ai./
- kazinator 7y agoThere is no way I would just plop that IPv6 regex into any serious program. :)
- sylvanaar 7y agoNothing will ever beat RegexBuddy when it comes to Regex tools. It is an entire IDE just for regex, and has been my not-so-secret weapon for a decade or more.
- samat 7y agoThis is very neat, thank you!
- chenster 7y agoFor email specific regular expression, it's all covered on https://emailregex.com https://emailregex.com
- qqqqqqqqq3 7y agoRegex: Good Idea Implemented Poorly
- shawnyou 7y agoGood tool
- Diti 7y agoFor the love of god, PLEASE DON’T USE REGEX TO VALIDATE EMAIL. The RegEx of this website ignores plus-addressing, for example. All you need to do to validate email is send a verification email.
- xiconfjs 7y agonot just the email regex is simplified (and at the end plain wrong). Also the one for phone numbers is highly simplified and will not match all valid phone numbers...
- deleted 7y ago[deleted]
- deleted 7y ago[deleted]
- hamid_ra 7y agolove the idea! I would crowdsource it so people can add their regex and vote on other people rexgexes!