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?

Latest comments (67)

Collapse
 
lewiscowles1986 profile image
Lewis Cowles

It's generated code, according to a declarative syntax, that has been tested and iterated on by millions of projects over decades... What isn't to love?

Prefer if we can use simpler things, but if it's needed. Then it is needed.

Collapse
 
voyeg3r profile image
Sérgio Araújo

As a vim/nvim user I would say that regex is my bread and butter. I also love using GNU grep, "Global REgular exPression Print", sed and a bunch of other programs that make use of it.

Collapse
 
joaogouveia profile image
joao-gouveia

Regex is difficult to read and can have unanticipated side effects. I tend to run from it if I can.

Collapse
 
syxaxis profile image
George Johnson

Very useful BUT it's almost an entire language in it's own right. I kind of respect that and just skirt around it. I use online regex builders when I need a regex for a filter style func call and that's me and regex done with until next time!

There was an 300 page O'Reilly book sold at one point just on Regex and nothing else! That's how deep the rabbit hole goes on regex. If you can avoid regex, then do so, if you can't then make the expression simple and watertight by testing the heck out it. Regex is greedy and if you don't test it properly you will regret it later.

Collapse
 
michaelkith profile image
MICHAELKITH (GitHub username)

Hello Developers

Collapse
 
lepinekong profile image
lepinekong • Edited

In the past I hate them and just searched copy and paste because it was pretty simple need but as I'm currently developping a Visual Meta Programming tool I'm obliged to really understand what I'm doing like Lookaround and Lookbehind so I'm now starting to like them more :)

Collapse
 
zakwillis profile image
zakwillis

I think they are great. They don't work in all situations.

ultrapico.com/expresso.htm is for .Net and a great tool for creating regex expressions, it doesn't support all elements of regex. Am not a super genius on them but they are an essential element of development for me when handling information.

Here is an example of configuration from a translation file from my application which is an awesome DevOps Deployment application am hoping to market in future.

The configuration below is to either find a value or a regex expression in an input file (typically itself an application configuration file) to clean a development configuration file. Sounds complex, but a great way of translating source configuration into something generic for different environmental deployment.

Regexes provides more control, but aren't always the best approach.

inforhino.co.uk/beta/automation-an...

                        {"IsRegex": false, "FindValue" : "C:\\\\InfoRhino\\\\cms\\\\IRWebsite\\\\IRWebsiteCMS\\\\wwwroot\\\\ClientConfiguration\\\\TConfigType.json", "ReplacementValue" : "{TargetServerRoot}\\\\ClientConfiguration\\\\TConfigType.json"}
                        ,
                        {"IsRegex": false, "FindValue" : "C:\\\\InfoRhino\\\\cms\\\\Adverts", "ReplacementValue" : "{TargetServerRoot}\\\\Store\\\\Adverts"}
                        ,
                        {"IsRegex": false, "FindValue" : "C:\\\\InfoRhino\\\\cms\\\\IRWebsite\\\\IRWebsiteCMS\\\\wwwroot", "ReplacementValue" : "{TargetServerRoot}\\\\wwwroot"}
                        ,
                        {"IsRegex": false, "FindValue" : "content\\\\Cards", "ReplacementValue" : "Cards"}
                        ,
                        {"IsRegex": true, "FindValue" : "(?is-nx:(?<=\"CardDomainHeaderCaption\"\\:(\\s{0,1})\")([a-z0-9\\s\\,\\.]+))", "ReplacementValue" : "{CardDomainHeaderCaption}"}
                        ,
                        {"IsRegex": true, "FindValue" : "(?is-nx:(?<=\"CardDescription\"\\:(\\s{0,1})\")([a-z0-9\\s\\,\\.]+))", "ReplacementValue" : "{CardDescription}"}
                        ,                       
                        {"IsRegex": true, "FindValue" : "(?is-nx:(?<=\"CompanyName\"\\:(\\s{0,1})\")([a-z0-9\\s\\,\\.]+))", "ReplacementValue" : "{CompanyName}"}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
davestewart profile image
Dave Stewart • Edited

regexone.com/ is BY FAR the best resource to learn RegEx from scratch.

Pretty much everything you would get in this book (but also worth reading!)
amazon.co.uk/Mastering-Regular-Exp...

And actually, core RegEx is fairly straightforward:

  • learn the tokens
  • read left to right
  • say what you see
  • (learn the tokens again)

Good luck!

Collapse
 
scorpit01 profile image
Anish Mahapatra

I'd rather master oop then master regex

Collapse
 
afrinc profile image
John Dears • Edited

Never liked it, I tried to but never.
While writing or reading some, for a very short while, I get this feeling that I understood it now , but next second here and there, gets confused again.

I guess its just one of those skills, where one would *either get it entirely or nothing at all.*
Writing regex especially is tricky for matching IPV4 addresses.
I hope I am not alone :/

Thank god for these parser/convertor tools.

Collapse
 
mondash profile image
Matthew Ondash • Edited

If you're trying to pattern match a string, is there really a better option? If so, I don't know it, but feel free to let me know.

There's definitely a rabbit hole to fall into and a lot of complexity along the way, but I've found regex to be one of the more reusable and powerful tools in my arsenal even though most of the time they seem to be on the simple side rather than complex.

If it ever get's confusing I've always got my goto: regex101.com/

Collapse
 
conw_y profile image
Jonathan • Edited

How do you feel about it?

Good. It's a useful tool.

Do you seek to use or avoid it as a problem solver

I treat it similarly to SQL or CSS – use it where appropriate, but try to keep it behind an abstraction if possible. E.g. I would wrap a phone number RegEx in a function such as isValidPhoneNumber.

If it's simple to solve the problem without a RegEx then I'll solve it without the RegEx. But sometimes a RegEx is simpler, e.g. the above example.

how much do you understand it

RegEx language generally – I know the basic concepts well enough, but I always check reference materials and/or use a RegEx tool when implementing one.

Specific RegExs – I almost never re-use a RegEx without first taking it apart and making sure I understand what's going on (same with any code snippet really).

Collapse
 
094459 profile image
Ricardo Sueiras

One of the key building blocks of tech over the past 30 years, but one that you still have to reach for the cheat sheet. Once you have written your first mod_rewrite rules, you never look back...

Collapse
 
cubikca profile image
Brian Richardson

I started on Perl, so I'm very familiar with regular expressions. That said, they definitely are not that easy to read, so for code, keep it simple and comment what you are doing. It solves a lot of problems, but not necessarily better than more readable approaches. Still, you won't completely avoid them. Rewrite rules, for example, still require a basic understanding of regular expressions, as well as some network device configurations.

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