DEV Community

Ben Halpern
Ben Halpern

Posted on

How do you feel about regex?

Regex seems to have a broad array of love and hate. How do you feel about it? Do you seek to use or avoid it as a problem solver, and how much do you understand it?

Oldest comments (67)

Collapse
 
waylonwalker profile image
Waylon Walker

Works great in my terminal and editor. I can search for something till it looks right, then replace it with what i want.

It production code its so hard to see all the edge cases. As a Data Engineer performance is about last on my list of metrics, as most of what I do is run on a schedule, not when someone clicks it, so an extra 5s on a 10 minute run is no big deal. With that said every time I see a regex in a code review I ask can we do this without a regex, even with a huge performance hit, or write tests for every possible edge case we can brainstorm.

Collapse
 
mbarzeev profile image
Matti Bar-Zeev

You write a regex and don't write a comment next to it describing what it does, you and I gonna have a lil' chat ;)

Collapse
 
waylonwalker profile image
Waylon Walker

I recently had a case where the person I was reviewing added regex, and they did comment what it did well, and honestly the comment looked exactly correct. They were missing tests, and it wasn't until they ran tests that they realized that what they had done was not what they described in the comment.

Collapse
 
mbarzeev profile image
Matti Bar-Zeev

Hey, tests go without saying, right?
;)

Collapse
 
zakwillis profile image
zakwillis

As long as it can be tested proven, and injectable (i.e. not hard coded) should be fine? :)

Collapse
 
sherrydays profile image
Sherry Day

It's something I use often, but still 100% only copy from Stack Overflow.

Collapse
 
drewclem profile image
Drew Clements • Edited

I don't write regex. I look up what I need to when I need it and I never think about it again. 🀣

I've found that the specificity of what I need when I do need it can vary so vastly, and that the library for it is so large, that the best use of my time is figuring out what I need at that specific moment and moving on.

It's not needed often enough to warrant full deep dive into it, for me at least.

Collapse
 
ben profile image
Ben Halpern
Collapse
 
rfornal profile image
bob.ts

I love working with Regular Expressions.

I use them frequently in VS Code and have a few articles out here on implementing them for Search-and-Replace.

I had a project where I had to replace an AS400 Custom Script Search Language with a JavaScript version. I quickly learned how slow they are when running hundreds of them per line. It also prompted me to create a new tool for documenting Regular Expressions (github.com/bob-fornal/reggie-docs).

Collapse
 
rgthree profile image
Regis Gaughan, III

Regex was my second programming β€œaha moment.” I love regex, all thanks to Apache’s mod_rewrite way back in like 2004.

Collapse
 
lexlohr profile image
Alex Lohr

I admit, when I first became really fluent in RegExp, I tended to overuse them for some time. Even now, I have the impulse to solve string-based issues with RegExp, but I have learned to stop and think about if they really are an improvement over other solutions. So you could say I seek to use it and try to avoid it at the same time, mostly because I understand it.

Collapse
 
mellen profile image
Matt Ellen • Edited

A few years ago I bought a book (I can't find right now) about the theory of computation, and it started with finite deterministic automata (FDA), moving on to non-deterministic finite automata (NDA) and how FDA and NDA are mathematically equivalent, then it showed how to turn NDA into regular expressions.

The explanation opened my mind to a new way to look at regex, so I found it a lot easier to understand and write them, and also understand their limitations.

The main issue I'm left with is that regex engines these days have added look-ahead and look-behind which means they are no longer mathematically equivalent to NDAs.

Collapse
 
link2twenty profile image
Andrew Bone

I like it but I tend to tell people to avoid it if possible just because it makes code hard to read.

If people on the team do use regex I ask them to include a link to regexper.com/ in a comment.

It generates a flow chart like this one.

regex flow chart

// https://regexper.com/#%2F%5E%5Cw%2B%28%5B-%2B.'%5D%5Cw%2B%29*%40%5Cw%2B%28%5B-.%5D%5Cw%2B%29*%5C.%5Cw%2B%28%5B-.%5D%5Cw%2B%29*%24%2F
const emailReg = new RegExp(/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/);
console.log(emailReg.test('user@place.com'))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
priteshusadadiya profile image
Pritesh Usadadiya

I didn't know about one. definitely useful. Thanks for sharing.

Collapse
 
baenencalin profile image
Calin Baenen

Oooh. I like this tool you introduced upon everyone.
Personally, if a regex is confusing me, I'll just pop over to regex101 for a quick test. It's also a great tool.

Collapse
 
lepinekong profile image
lepinekong • Edited

I prefer regexr.com because regex101 is not embeddable in figjam document figma.com I use to keep code notes as it supports code blocks.

Other tools I find pretty but buggy for complex regex are :

regexper.com/
jex.im/regulex
extendsclass.com/regex-tester.html

Collapse
 
andypiper profile image
Andy Piper

Now, this is really cool and useful - thank you!!

Collapse
 
jesusantguerrero profile image
Jesus Guerrero

Wow thanks for sharing that tip.

Collapse
 
lepinekong profile image
lepinekong • Edited

Have tried them all I think, that one is nice but it doesn't work for very complex regular expressions (like with some lookaround expressions) whereas it works with regexr.com

Collapse
 
internetmosquito profile image
Alejandro Villamarin

didn't know that service, seems awesome, also worth a try cucumber.io/docs/cucumber/cucumber..., makes regular expressions more human friendly and readable

Collapse
 
jeremyf profile image
Jeremy Friesen

I love using regular expressions. And try to adopt the following pattern:

  1. Write a method for evaluating the regular expression.
  2. Bombard that method with tests, both matching and non-matching cases.
  3. Profit!

Regular expressions can be quite dense to read, so I want to use the tests to highlight my expectations around what I wrote.

Collapse
 
phantas0s profile image
Matthieu Cneude • Edited

If I can avoid it in my code, I will. They can grow like crazy overtime, and they become difficult to read and understand.

I love them in my editor (I use Vim btw) or to perform operations in the shell, however. It's super powerful for everything plain text: search, search and replace, repeating an action on specific lines... the list goes on.

Collapse
 
qarunqb profile image
Bearded JavaScripter

I like to use Regex and I use it frequently but not for anything too complicated. I generally build all my RegEx from Regex101.

I really understood it at its core during my university Computer Science Program where we talked about DFAs -> NDFAs -> Regular Languages -> Regular Expressions

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao

Hmmm...I would advise to use it only for search, data scrapping and data validation/cleaning purposes. Beyond that I won't advise anyone to use it unless there's a real reason to use it like to reduce the complexity of your code for the above purposes.

Plus there's tons of websites that allows you to copy and paste regex for specific uses depending on your language of choice or regex engine you are using.

To me it's sort of like Leetcode, readability is a issue unless you had learnt it but beyond that it kind of being too clever with your code.

Collapse
 
sameervanjari profile image
Serious Sam

I like how regex makes it easier with the task, but personally I really don't understand it most of the time. But I will try my best to learn it.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.