271

you are viewing a single comment's thread
view the rest of the comments
[-] backhdlp@lemmy.blahaj.zone 41 points 1 year ago

I still don't understand regex at all

[-] d_k_bo@feddit.de 61 points 1 year ago

I recommend using https://regex101.com/

It explains all parts of your regex and highlights all matches in your example text. I usually add a comment to a regex101 playground if I use a regex in code.

[-] 18107@aussie.zone 41 points 1 year ago

The plural of regex is regrets.

[-] tostiman@sh.itjust.works 39 points 1 year ago

Regexes are write-only. No one can understand other peoples regexes

[-] Bene7rddso@feddit.de 7 points 1 year ago

Not only other people's regexes. Mine from last week and before too

[-] Tathas@programming.dev 3 points 1 year ago

That's also 'cause other people's regex are garbage!

load more comments (15 replies)
[-] FUsername@feddit.de 24 points 1 year ago

My guess is, that someone started with a small share of features to find a simple solution for the problem, but the complexity of the problem got waaaay out of hand.

[-] Prunebutt@feddit.de 24 points 1 year ago

Regexes are actually used in formal computer science (if that's the right term), i.e. "proof that this and that algorithm won't deadlock" or something like that.

They're actually really elegant and can cover a lot. But you'll have to learn them by using them.

[-] naptera@feddit.de 5 points 1 year ago* (last edited 1 year ago)

For the purpose of algorithm verification, the final and/or pushdown automaton or probably sometimes even Turing Machines are used, because they are easier to work with. "Real" regular expressions are only nice to write a grammar for regular languages which can be easily interpreted by the computer I think. The thing is, that regexs in the *nix and programming language world are also used for searching which is why there are additional special characters to indicate things like: "it has to end with ..." and there are shortcuts for when you want that a character or sequence occurs

  • at least once,
  • once or never or
  • a specified number of times back to back.

In "standard" regex, you would only have

  • () for grouping,
  • * for 0 or any number of occurances (so a* means blank or a or aa or ...)
  • + as combining two characters/groups with exclusive or (in programming, a+ is mostly the same as aa* so this is a difference)
  • and sometimes some way to have a shortcut for (a+b+c+...+z) if you want to allow any lower case character as the next one

So there are only 4 characters which have the same expressive power as the extended syntax with the exception of not being able to indicate, that it should occur at the end or beginning of a string/line (which could even be removed if one would have implemented different functions or options for the tools we now have instead)

So one could say that *nix regex is bloated /s

[-] loafty_loafey@lemmy.world 3 points 1 year ago

You are probably thinking of Temporal logic which allows us to model if algorithms and programs terminate etc! It can be represented by using state machines tho!

[-] Prunebutt@feddit.de 2 points 1 year ago

It's been a while, so I'm quite rusty, especiallyeon the terminology, but I think we modelled feasible sequences of finite and infinite state machines using regexes.

That's how I was forced to learn 'em in uni. ;)

[-] loafty_loafey@lemmy.world 2 points 1 year ago

Ohhh I must be confusing the two! But yea, Regex is just NDAs in text form! The course I have had on it is one of my favorites! Really fun stuff

[-] loafty_loafey@lemmy.world 20 points 1 year ago

Regex is actually just a way to write (Epsilon) non determistic state automata(ε-NDA) using text! ε-NDA comes from automata theory and they are just a somewhat powerful way to describe state machines! They can kind of be seen as a stepping stone to things like Context-Free Grammars which is what language parsers use to define their language/parsers, and Turing machines! Regex is a fundamental part of computer science, and they are of course incredibly useful in string validation due to their expressive power! If you study at uni and get the chance to take a course in automata theory I recommend it! Personal favorite subject :)

[-] FooBarrington@lemmy.world 16 points 1 year ago

It's really not too bad as long as:

  • you use a proper IDE (e.g. Regex101) with highlighting and piecewise explanations

  • you use named capturing groups

  • you give the regex a descriptive name in your code

[-] blackbirdbiryani@lemmy.world 3 points 1 year ago

People write regex in notepad and complain it doesn't work on the first try...

[-] mindbleach@sh.itjust.works 12 points 1 year ago

It's pattern-matching. Like searching *.txt to get all text files. It's just... more. There's symbols for matching the start of a string, the end of a string, a set of characters, repetition, etc. Very "etc." And the syntax blows. The choices of . for match-any-character and * for zero-or-more really fuck with common expectations.

It can also replace substrings that match. Like changing the file extension of all text files. Where it gets properly difficult is in "capture groups." Like looking for all file extensions, and sticking a tilde after the dot. You can put parentheses around part of the pattern being matched and then reference that in the replacement. Conceptually simple - pain in the ass to use properly - syntax both sucks and blows.

Lookahead is what you do to match "ass" but not "assault." I refuse to elaborate further.

[-] qaz@lemmy.world 6 points 1 year ago

What part do you not understand?

[-] BestBouclettes@jlai.lu 10 points 1 year ago
[-] qaz@lemmy.world 6 points 1 year ago

So you do not understand nothing? That's great to hear.

[-] alphafalcon@feddit.de 3 points 1 year ago

No, No, they don't understand everything and nothing!

[-] qaz@lemmy.world 3 points 1 year ago

Both statements match 😄

[-] Norgur@kbin.social 5 points 1 year ago

//////?-.,", duh,?!

this post was submitted on 12 Nov 2023
271 points (95.6% liked)

Programmer Humor

32483 readers
493 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS