Jump to content

How to learn / use regex ?


Pegasus

Recommended Posts

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-649dc1c3f285
https://regexone.com/
https://www.regular-expressions.info/tutorial.html
https://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 by Explv
  • Like 1
Link to comment
Share on other sites

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 by Explv
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Explv
  • Like 3
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...