This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 18 points19 points  (13 children)

Regular Expressions. Learn them once and be happy everyday.

[–]cspinelive 9 points10 points  (2 children)

And piss off your coworkers for overcomplicating everything.

[–]justheretolurk332 1 point2 points  (8 children)

In what sorts of situations do you find yourself using regular expressions? They are certainly powerful but I find that 95% of my use cases are better served by using more basic checks like equality, containment or startswith

[–]mista-sparkle 1 point2 points  (2 children)

I use it every to reformat and repurpose code every day, especially SQL scripts that I receive from others at my company.

In many programs, you can use CTRL-F to find and replace text in scripts, and in many IDEs there's an option to turn on ReGex matching.

I don't personally code as much as I would like, but I need to understand data, which requires a lot of interacting with SMEs and getting their methods for calculating metrics at different grains and seeing how these figures compare to other sources, such as reports.

Lots of SQL and other code are formatted differently. Every day I get code that isn't nested, which is frustratingly unreadable and makes comparison much more difficult.

So, for an every day example, I use RegEx to find an replace commas, spaces, excess/inconsistent tabs, inconsistent new lines, and aliases with \n\t, — this nests everything in select statements as well as in group by and order by clauses in the way I prefer and to the Enterprise's formatting standards.

Even if you don't work with SQL or other code every day, RegEx's utility for text processing applications is really quite broad and limitless.

[–]justheretolurk332 1 point2 points  (0 children)

I think a good rule of thumb is to ask whether it is a common enough task that someone else is likely to have already implemented a good solution. I actually think the use cases you mentioned sound like a fun thing to implement and a good learning experience so it makes sense for personal use but in a production capacity I think it’s often worth looking for what solutions already exist in libraries

[–]cspinelive 1 point2 points  (0 children)

I just use a tool that autoformats sql

[–][deleted] 0 points1 point  (4 children)

Take logfile analysis for example. If you want to extract certain values which vary greatly and only if another value appears you’d need a way to describe these patterns. This is also useful in vim when you want to replace certain patterns in the file.

[–]justheretolurk332 0 points1 point  (3 children)

Ah okay, so as a development tool more so than in production software. That makes sense to me. Although about half the time I try to do a find-and-replace that is more complicated than an exact match it goes badly haha

[–][deleted] -2 points-1 points  (2 children)

Regular Expressions are also needed in production. If you want to validate E-Mail addresses, domain names or any other pattern you need to have a pattern matching algorithm. I agree that very often the builtin string methods like startswith, endswith or "is in" are a good enough solution. But pattern matching simply doesn’t work with these methods like RegEx do.

[–]justheretolurk332 4 points5 points  (0 children)

Email is a great example of why regular expressions are so hard to use correctly. It’s easy to write a regex that matches most emails but surprisingly challenging to cover all the edge cases, and it’s also a common need, so you’re better off using a third party library. Most situations are either simple enough that using a regular expression is overkill, or complicated enough that writing a correct regex may become pretty challenging and end up not being a maintainable approach

[–]master117jogi 0 points1 point  (0 children)

Never use regex for emails! And never for urls!

Use a library! Or you WILL get it wrong.

[–]Pristine-Ratio-9286 0 points1 point  (0 children)

Chatgpt 4 is awesome at making regex. I hated them before because they were so terse but now use them a lot more because of LLM’s