Pegasus Posted June 10, 2018 Share Posted June 10, 2018 I tried to learn regex years ago but failed. How to learn / use regex ? Any good site recommend? Quote Link to comment Share on other sites More sharing options...
Charlotte Posted June 10, 2018 Share Posted June 10, 2018 https://docs.oracle.com/javase/tutorial/essential/regex/ 1 Quote Link to comment Share on other sites More sharing options...
Pegasus Posted June 10, 2018 Author Share Posted June 10, 2018 (edited) .......... thanks Edited June 10, 2018 by Pegasus Quote Link to comment Share on other sites More sharing options...
Alek Posted June 10, 2018 Share Posted June 10, 2018 I mean try practicing some basic string manipulation: -Validate if a string is a valid ip address/phone number -Remove numbers and replace uppercase with lowercase letters in a string 1 Quote Link to comment Share on other sites More sharing options...
Explv Posted June 10, 2018 Share Posted June 10, 2018 (edited) One tip for when you are learning is to just use a site like https://regex101.com/ instead of using a programming language. It's faster to change your pattern, you can see the output more clearly, and can easily tell what's going wrong. As for tutorials... let me google that for you... https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285https://regexone.com/https://www.regular-expressions.info/tutorial.htmlhttps://www.rexegg.com/https://ryanstutorials.net/regular-expressions-tutorial/http://www.vogella.com/tutorials/JavaRegularExpressions/article.html Regex for basic strings is trivial, and is clearly explained in the thousands of tutorials online.. Edited June 10, 2018 by Explv 1 Quote Link to comment Share on other sites More sharing options...
ThatGamerBlue Posted June 10, 2018 Share Posted June 10, 2018 (edited) Kinda related, I use https://regexr.com to test my regex expressions, although I rarely use them, poor performance. e: https://regexr.com/3qqd1 Edited June 10, 2018 by ThatGamerBlue Quote Link to comment Share on other sites More sharing options...
Explv Posted June 10, 2018 Share Posted June 10, 2018 (edited) 23 minutes ago, ThatGamerBlue said: I rarely use them, poor performance. Never prematurely optimise. In the majority of use cases it is absolutely preferred for someone to use a simple, succinct regex pattern, rather than write x lines of code. The x lines of code might execute 50ms faster, but who cares, I would rather read the code faster. I would only use something other than regex when I *know*, through profiling or through common sense, that removing it would result in a noticeable impact in overall run time. You can use regex to search for patterns in millions of strings, and the performance will still be great. It really depends on the pattern you are using, and the size of the input. Whether to use regex or not should be decided on a case-by-case basis. Edited June 10, 2018 by Explv Quote Link to comment Share on other sites More sharing options...
ThatGamerBlue Posted June 10, 2018 Share Posted June 10, 2018 9 minutes ago, Explv said: Never prematurely optimise. In the majority of use cases it is absolutely preferred for someone to use a simple, succinct regex pattern, rather than write x lines of code. The x lines of code might execute 50ms faster, but who cares, I would rather read the code faster. I would only use something other than regex when I *know*, through profiling or through common sense, that removing it would result in a noticeable impact in overall run time. You can use regex to search for patterns in millions of strings, and the performance will still be great. It really depends on the pattern you are using, and the size of the input. Whether to use regex or not should be decided on a case-by-case basis. One of my work projects had a regex that took one second to compile, ~250 ms per string and was being run twice every page load so I kinda hate regex from then. Also 50 ms is a lot of time in the computing world. Quote Link to comment Share on other sites More sharing options...
Explv Posted June 10, 2018 Share Posted June 10, 2018 (edited) 28 minutes ago, ThatGamerBlue said: One of my work projects had a regex that took one second to compile, ~250 ms per string and was being run twice every page load so I kinda hate regex from then. Also 50 ms is a lot of time in the computing world. That sounds like a case where you have a really, really bad regex pattern, which was identified to be a performance bottleneck, and was optimised. It doesn't mean you should avoid regex everywhere. It seems pretty weird that you would be doing such an operation on page load anyway. 50ms was just a number I pulled out of thin air, and whether or not 50ms is a long time depends on the application. I work on systems that load millions of lines of data every day, and take hours to complete. If I write a regex pattern that is run once and takes 1 second to complete (which is pretty uncommon), the performance of it is irrelevant to the overall performance of the system. If a regex is taking 1 second for a simple operation, then yes, you should probably use something else, or investigate whether there is a better way to write the pattern. But it really isn't very common that a regex pattern takes 1 second to run on a single input (unless that input is absolutely gigantic) Anyway, you do you. I'm just a big fan of maximizing readability first, and optimizing later Edited June 10, 2018 by Explv 3 Quote Link to comment Share on other sites More sharing options...
Qubit Posted June 10, 2018 Share Posted June 10, 2018 Maybe overkill but look up some classes on Foundations of Computer science or Computer theory, specifically topics in regular expressions and finite state automata. If you're like me and like the theory behind CS concepts, that's the specific area that deals with regex. Then move on to language-specific syntax and implementations. Quote Link to comment Share on other sites More sharing options...