Hacker News new | past | comments | ask | show | jobs | submit login

Can someone elaborate on how they use reg ex as a productivity boost?

Do you use it for text transformation? How do you use it exactly? Do you use it as code itself or as a tool to help you explore/write code?

I am a huge fan of Macros, I 100% understand their role in text transformation, but what problem does regex solve? I spend so much time trying to remember the correct syntax, it only ever works for me when solving a specific code problem, but Ive never used it in one-off workflow problems.

Curious if I have missed some huge place where it is helpful.




>> Do you use it for text transformation?

Mostly, yes. How often do you search and/or replace and/or extract text? Now imagine doing it while being able to specify patterns instead of just fixed text.

A couple examples:

- find all occurrences of some text only when near some other text, no matter the intervening text;

- transform calls to a routine to calls to another routine, while swapping arguments.

As for other tools, once you know them, you will find ways to use them.

Yes, their syntax can be difficult to remember, but most of the time you are going to use just a few patterns and will learn them by rote memorization. They will become second nature, and indeed I myself am sometimes surprised by the complexity of patterns that I write without thinking about it. Anyway, I recommend that you have handy a plugin for your text editor that highlights matches, to help in troubleshooting buggy regexes when they get convoluted.


Regexes are a bit of a double-edged sword. I rarely use them in code - I often find it more productive (and maintainable) to simply type out the verbose code for doing the string transformations. This depends on how good you are at writing/interpreting regexes and on what text manipulation tools your programming language/stdlib gives you. I imagine a compiled regex can often outperform naively-written code, though.

On the other hand, regexes saved my skin a couple of times when I needed to search/extract/modify/format a large number of text files (or one extremely big one). I suck at command line, so I usually open the file/folder in vscode and use their search/replace function. It's super useful for these "one shot" text operations.


I use them when there is some data hidden away with an unresponsive team as far as fetting a good api to read the data, and i don't have a lot of time, so i will just toss together a quick scraper from however the data is exposed (could be html, could be csv, could be some terrible json or xml format). I will use regexp, but even more crap like line.contains() or .endswith or line.split("nc]")[1] or json.get('whatevs',[{}])[0].get('sub1', {}) etc.

Dreadful, but fast enough to let everyone know, there are 58 Java 7 taking prod load within an hour of the meeting where it was assumed there are zero Java 7 apps.


Editing large amounts of data. Notepad++ supports (some) regex find and replace, and I use it often to clean large lists or reformat data.


Thanks for mentioning that this is in Notepad++. I've only ever used macros for line editing, never even tried regex search and replace. I gave it a shot and it is incredibly powerful.

Wow! Thank you.


A good text editor will have regex ability, and once you learn it, it’s super convenient to have it handy. Writing macros to do stuff is more of a hassle. RegEx is most useful for me as a super quick and easy way to solve a one off problem that isn’t important enough to write code to solve.

I once walked in on a coworker (who’s not a developer) who had copied a pdf into text and was laboriously scrolling through thousands of lines of text to remove footer text with page numbers. He spent hours doing this, it’s a 10 second problem with using regex.

In the same job, sometimes our clients would give me large files in the wrong format (like a word document) that couldn’t be normally be import. Notepad++ and some regex magic pulled out all the data I cared about in a minute. It would have taken years if my other coworker did it his way.


Thanks for mentioning that use case of a footer with page numbers. I can understand how a RegEx would be powerful there.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: