last time when did you use Regular Expressions?
from kionite231@lemmy.ca to programming@programming.dev on 04 Jul 2024 11:58
https://lemmy.ca/post/24392099

#programming

threaded - newest

verstra@programming.dev on 04 Jul 2024 21:04 next collapse

Today, to configure fail2ban. Before that, yesterday to select which tests to run.

nik9000@programming.dev on 04 Jul 2024 22:59 collapse

Usually I use glob patterns for test selection.

But I did use reges yesterday to find something else. A java security file definition.

FizzyOrange@programming.dev on 04 Jul 2024 21:24 next collapse

We use it for triaging test failure (running tens of thousands of tests for CPU design verification).

That use is acceptable because it is purely informational. In general you should avoid regexes at all costs. They’re difficult to read, and easy to get wrong. Generally they are a very big red flag.

Unfortunately they tend to get used where they shouldn’t due to lazy developers not parsing things properly.

Dropkick3038@programming.dev on 07 Jul 2024 19:38 collapse

regexes are a well established solution for parsing strings. what exactly is the “proper” alternative you propose?

spartanatreyu@programming.dev on 08 Jul 2024 02:01 collapse

There are some tools/libraries that act as a front-layer over regex.

They basically follow the same logic as ORMs for databases:

  1. Get rid of the bottom layer to make some hidden footguns harder to trigger
  2. Make the used layer closer to the way the surrounding language is used.

But there’s no common standard, and it’s always language specific.

Personally I think using linters is the best option since it will highlight the footguns and recommend simpler regexes. (e.g. Swapping [0-9] for \d)

spartanatreyu@programming.dev on 05 Jul 2024 00:02 next collapse

At least once every few days while coding, usually to do one of the following:

  1. Select multiple things in the same file at the same time without needing to click all over the place

    Normally I use multicursor keyboard shortcuts to select what I want and for the trickier scenarios there are also commands to go through selections one at a time so you can skip certain matches to end up with only what you want.

    But sometimes there are too many false matches that you don’t want to select by hand and that’s where regex comes in handy.

    For instance, finding:

    • parent but not apparent, transparent, parentheses, apparently, transparently
    • test but not latest, fastest, testing, greatest, shortest
    • trie but not entries, retries, countries, retrieve
    • http but not https

    … which can be easily done by searching for a word that doesn’t include a letter immediately before or immediately after: e.g. \Wtest\W.

  2. Search for things across all files that come back with too many results that aren’t relevant

    Basically using the same things above.

  3. Finding something I already know makes a pattern. Like finding all years: \d{4}, finding all versions: \d+\.\d+\.\d+, finding random things that a linter may have missed such as two empty lines touching each other: \n\s*\n\s*\n, etc…

zero_spelled_with_an_ecks@programming.dev on 05 Jul 2024 04:37 next collapse

Yesterday. Gotta grep those logs.

Kissaki@programming.dev on 05 Jul 2024 06:15 next collapse

Earlier this week for a character range.

/edit: Now I remember. For setting up a new entry in Jenkins CI build failure analysis - identifying the build failure cause in the log.

livingcoder@programming.dev on 05 Jul 2024 11:28 next collapse

Yesterday doing a search using vim for a class that shared a lot of characters at the front with many other classes: /Bas.*Some I could have done a more precise search with better regex, but this was quick, easy, and worked.

towerful@programming.dev on 05 Jul 2024 12:41 next collapse

Interesting to see a lot of these responses (so far) are workflow related instead of being used in production.

sukhmel@programming.dev on 06 Jul 2024 14:52 collapse

Probably, because in production there are really few things that are best done with regex. Most use I had for regex in production is filling in data from user-provided files with specifically crafted names, and even there there was some guesswork because of errors in naming, and the same thing may have been achieved without regex by splitting and/or iterating

Dropkick3038@programming.dev on 05 Jul 2024 12:58 next collapse

Today.

NostraDavid@programming.dev on 05 Jul 2024 13:26 next collapse

Yesterday, when I had a file with a list of JSON objects, and I wanted to move the date field at the end to the beginning, so I used regex find and replace to move it. Something like \{(.*?), (“date”: “.*?”) in Search, and then {$2, $1 in replace (or something close to it).

Yes, I refactor code and data using regex. I can’t be arsed to learn AWK (even though I should).

kionite231@lemmy.ca on 05 Jul 2024 15:36 collapse

AWK doesn’t work with json IIRC. You have to use jq to deal with json.

NostraDavid@programming.dev on 06 Jul 2024 20:38 collapse

While yes, the way I had it structured looked like a CSV if you squinted a little, I do fully agree AWK can’t be used for just any old JSON.

jq is dope, but that language still feels pretty confusing IMO.

expr@programming.dev on 05 Jul 2024 14:10 next collapse

Every day pretty much with Unix tools. Vim, awk, sed, etc.

borup@programming.dev on 05 Jul 2024 14:52 next collapse

Usually many times a day… Even today which have been mostly meetings.

Thcdenton@lemmy.world on 05 Jul 2024 13:19 next collapse

Writing the script that got me fired

kionite231@lemmy.ca on 05 Jul 2024 15:33 next collapse

I hope you are joking

verstra@programming.dev on 05 Jul 2024 20:40 collapse

Please explain more! What happened?

Did you destroy a database? Expose credentials? Nuke the company intentionally?

DeprecatedCompatV2@programming.dev on 05 Jul 2024 16:31 next collapse

Yesterday, for capturing URLs.

kionite231@lemmy.ca on 05 Jul 2024 16:35 collapse

https?//[a-zA-Z0-9_-]*

I am kinda learning RE right now 😅

DeprecatedCompatV2@programming.dev on 05 Jul 2024 17:08 collapse

What about ftp? 🤔

kionite231@lemmy.ca on 05 Jul 2024 17:48 collapse

If we want to include every protocol then the RE could be complex.

sukhmel@programming.dev on 06 Jul 2024 14:47 collapse

Depending on the use-case it maybe should. On the other hand, some things are better left to library implementations rather than custom regex, e.g. email validation

bitcrafter@programming.dev on 05 Jul 2024 16:59 next collapse

I don’t always use regular expressions, but when I do, I use it to parse XML,

kionite231@lemmy.ca on 05 Jul 2024 17:57 collapse

iirc using RE to parse tag languages is not recommended.

bitcrafter@programming.dev on 05 Jul 2024 18:06 collapse

Sure, but if you are not regularly expressing code that has the potential of summoning elder gods that will swallow your soul into a dimension of ceaseless screaming then are you really living?

lud@lemm.ee on 05 Jul 2024 18:32 next collapse

A few hours ago.

I just wanted to make a list of AD group names into a powershell array.

lysdexic@programming.dev on 06 Jul 2024 08:20 next collapse

Asking this question is like asking when was the last time you had to search through text.

corsicanguppy@lemmy.ca on 06 Jul 2024 09:28 next collapse

This sentence is the uncanny valley for structure.

Oneironaut21@ani.social on 06 Jul 2024 18:55 next collapse

On average I’ve probably had to work with them or write one from scratch only a handful of times per year over my career. Not often enough to be an expert or anything but I’m not so afraid of them as I used to be.

tiredofsametab@kbin.run on 08 Jul 2024 02:11 next collapse

Actually writing code that uses them: last month. Commandline: last week.

obbeel@lemmy.eco.br on 29 Jul 2024 23:06 collapse

I used it to check a user input format.