DEV Community

Nabil Tharwat
Nabil Tharwat

Posted on

What's your biggest struggle with Regular Expressions?

There seems to be a shared struggle when it comes to Regular Expressions. Some suffer readability, some think it's completely useless, others find it difficult to learn. What do you think? What are struggles you face with Regular Expressions?

For those with no problems with it, what would you give as advice? What helped you grasp them?

Top comments (4)

Collapse
 
grahamthedev profile image
GrahamTheDev • Edited

regex101.com <- that is without doubt one of, if not the best ways to learn Regular Expressions.

I think the issue is that they look complicated and it is really easy to make a mistake, a tool like the above breaks down the steps you have written, explains them and lets you test against some text so you can check it matches.

Also a lot of people get confused because they try and use regular expressions to parse HTML....your will always end up in a mess with that! (if you have never seen the famous StackOverflow post on using a RegEx to parse HTML then follow that link!)

I mean, they also look horrible sometimes and I think that makes people think they use some kind of voodoo to work....

/((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^```

 *(\w*)\n([\s\S]*?)\n

```$)|((?:(?:^|\n+)(?:\t|  {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|(  \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm
Enter fullscreen mode Exit fullscreen mode

That right there is a regular expression to parse Markdown as part of a library called snarkdown, and it kind of works! (it might not display well on dev.to comments due to the fact it contains a load of markdown but you get the idea!)

But trying to get your head around it requires breaking it down into tiny sections, in which case it isn't so scary (but still difficult!)

Above all - get into developer mode when writing a RegEx - break the problem down into tiny steps and tackle each one at once.

Collapse
 
cescquintero profile image
Francisco Quintero 🇨🇴

For me is that I've used them only in rare instances so that every time I need to relearn the syntax.

Collapse
 
andrewbaisden profile image
Andrew Baisden

The fact that I have to keep referring to the documentation because I have not learned it fully 😅

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Without a doubt the best regex cheatsheet ever... I never leave home without it! 😁 Regex Cheatsheet