linux

This magazine is from a federated server and may be incomplete. Browse more on the original instance.

juli, in My First Regular Expressions

That’s cool! Kudos!

My biggest project was to remove leading and trailing whitespaces but I think I failed twice 😅

harsh3466,

🤣

I went though about 20 iterations to get all of this to work correctly.

NegativeLookBehind,
@NegativeLookBehind@kbin.social avatar

Why spend 20 minutes manually changing text in a file, when you can spend 90 minutes figuring out a single RegEx to do it?

harsh3466,

So much truth here.

sxan, in Why do you use the terminal?
@sxan@midwest.social avatar

Scriptability.

Venat0r, in Why do you use the terminal?

Because its easy to make a script that can chain together a bunch of commands for tasks i do frequently, so its only one manual step to do it.

Tetsuo, (edited ) in My First Regular Expressions

Good job !

I highly recommend trying out the various online regex editor.

These WISIWIG kind of editors are great because you immediately see what the regex is catching and for what reason.

I took the first one in my search results but try different ones.

regex101.com

Also I used GPT to get some regex for some specific strings and it can be helpful to get a quickstart at building a specific regex.

In that case I was building a regex for a specific log from postfix.

PS: just make sure to select the correct flavor of regex you are using in these online tools.

Edit: Also one of my favorite YT channels has pretty cool videos on RegEx : youtu.be/6gddK-cOxYc?si=0bnNkSDzifjdxwjU

malijaffri,

Piggybacking onto this to mention my go-to online RegEx editor: RegExr. It lets you test the regex as you type, explains the particular symbols used, as well as has a sidebar where you can see different pattern types categorically. I’ve been using it for almost 2 years now, and haven’t had any reason to use much else (after I discovered this).

harsh3466, (edited )

Thank you very much. I will definitely check out the regex builders. That’ll be super useful

Edit: fix stupid autocorrect turning regex into Reyes.

virku,

Wait. Are there flavors of regex? Every time I have to use regex it hurts my brain and I never need to do it enough to actually sit down and learn it properly like OP is doing. Just knowing there are different ways of doing the same things in an already mind baffeling language blows me away even more.

ricecake,

Yes. Most things use pcre, or Perl Compatible Regular Expressions, but there are other flavors. Usually they lack features or have slightly different syntax.

remotelove, (edited )
@remotelove@lemmy.ca avatar

Yeah. The only one you really need to care about (especially under Linux) is PCRE, the good 'ol Perl Compatible Regular Expressions. For the most part, every other flavor is a derivative of that. Microsoft had a weird version for a while, but that may be completely dead now, thankfully.

Learning the syntax of regex is fairly easy. Hell, I still have to use this cheat sheet more often now that my perl skills are no longer needed or even relevant.

Regex isn’t that hard. The challenge is identifying and understanding patterns in the data that you are filtering. Here is a brain hack: As an example, if to have pages and pages of logs that you need to filter, open up one of the log files, stare at the screen and hold the page down key for several dozen pages. Patterns can be easily seen in the blur of text that is quickly scrolling across the screen. (Our brains love to find patterns in noise, btw.) The patterns that you see will give you focus points for developing regular expressions to match. ie: You start breaking strings into chunks and seeing the ebb and flow of data streaming across a screen helps. Anomalies in the data “stream” are are easy to spot as well.

From a security and efficiency standpoint, you should also understand where the most processing takes place so you don’t kill whatever platform you are working on.

Sorry for the rambling, but I am getting older and feel the need to pass on a ton of tips and tricks whenever I can for these “archaic” languages.

harsh3466,

That screen scrolling tip is gold. I’ve often used that trick to spot anomalies in data. Hadn’t considered using it to spot the patterns for regex.

virku,

Thanks for the comprehensive reply! I have only used it for quite simple things like getting the id’s out of log lines where this and this key word exist. Great tip about pattern searching!

Merry Christmas

bizdelnick, (edited )

The only one you really need to care about (especially under Linux) is PCRE,

Well, no. sed, grep, awk, vi etc. use POSIX regexes. GNU implementations also provide perl compatible mode via an unportable option. In modern programming languages like go and rust standard regex engines are compatible to RE2 - relatively new dialect developed in Google that is not described in the Friedl’s book (you may think of it as an extension of extended POSIX dialect). Even raku has its own dialect incompatible to perl as well as other ones.

Nowadays it is common to move away from perl-like engines, however they are still widely used in PCRE based software and software written in python, JS etc.

remotelove,
@remotelove@lemmy.ca avatar

POSIX? Never heard of her.

While you are likely 100% correct, the legacy perl developer side of me is making nasty comments to you with illegible syntax.

bizdelnick,

Perl has introduced powerful backtracking regexes that were widely adopted. However they can be damn slow in some cases, that’s why RE2 refused backtracking while using some perl-like elements. Both basic and extended POSIX regexes are also non-backtracking because they are older than perl.

harsh3466,

Computerphile! I’ll check those out.

fuckwit_mcbumcrumble,

Regex101 is amazing. It tends to balk at backtracing which we rely on a lot for work, but it’s such a good visual.

Chat GPT can also save a lot of time writing regex, but it tends to write very unreadable regex because it thinks it’s being clever when it really isnt.

Regex is an art form, and writing readable regex is another step above that.

leo, in My First Regular Expressions

Knowledge and understanding. Feels good, man.

Obligatory Xkcd.

https://sh.itjust.works/pictrs/image/d6d2db0a-cf5a-4270-b4bb-a898d9b88695.png

harsh3466,

It does feel good! And thanks for that xkcd! That one’s new to me.

CosmicTurtle,

Ah…the days when perl was the shit and python was still a glimmer in the eye of some frustrated programmer.

bionicjoey,
stepanzak, in My First Regular Expressions

That’s really cool! I know some regex and I tried to learn vim regex, only to find out it’s a rabbithole so deep I’m afraid to look into. The feeling when you press enter and your carefully crafted regex does exactly what it’s supposed to do is awesome though. Good luck!

harsh3466,

Vim is on my list of things to learn. I didn’t even know vim had its own regex, but I suppose that makes sense. I’ve messed with vim a bit, but have stuck to nano so far.

stepanzak,

You might also like micro

adespoton, in My First Regular Expressions
harsh3466,

That looks like a great way to practice

adespoton,

It’s definitely a way to get your regex-fu to the next level, especially if you have people to compete against.

harsh3466,

Oh gosh. There are regex competitions out there, aren’t there.

adespoton,

Yup, including for the largest “in production” regular expression….

davel, in My First Regular Expressions
@davel@lemmy.ml avatar

“regex” means “regular expression”, so “regex expression” means “regular expression expression”.

harsh3466,

Dang! I read through my post three times to make sure I didn’t do that and completely missed that I did it right in the title. (Now fixed).

mcepl, in How can I migrate my existing /home/ directory to another drive?
@mcepl@lemmy.world avatar
  1. Many Linux installers can preserve /home when asked nicely.
  2. (as root) rsync -avz /home/youruser/ other-machine:/home/
OpenStars, in My First Regular Expressions
@OpenStars@kbin.social avatar

Regexps are awesome! And also not at the same time:-P. 🎉 Congrats👏!:-)

harsh3466,

Thank you!

mcepl, (edited ) in My First Regular Expressions
@mcepl@lemmy.world avatar

Give a man a regular expression and he’ll match a string… teach him to make his own regular expressions and you’ve got a man with problems. – yakugo in regex.info/blog/2006-09-15/247#comment-3022 (and yes, it is http:// never https:// for this domain)

harsh3466,

Guess I’ve got problems!

olafurp, in Why do you use the terminal?

I run stuff in the terminal because it’s nicer than clicking. It’s like a shortcut-only way of interacting with the computer when you get into it.

I make aliases and bash functions for everything I do often and is tedious to type / click like running steam games.

prowess2956, in My First Regular Expressions

I think the most impressive part of this is that your wife cares.

...does she have a sister?

sab,
@sab@kbin.social avatar

I'm currently seeing a girl I started dating after she had problems with her regex and I helped her out.

So far so good.

harsh3466,

She does but, I’d stay away from the sister. 🤣

Thorry84, (edited ) in My First Regular Expressions

I can also recommend the book the TS mentioned, it is very good and after reading it you will understand regular expressions. It’s fine to use a cheat sheet if you want, cause if you don’t do it regularly the knowledge can sag, but the understanding is what matters. Also depending on the context, different implementations can have slightly different syntax or modifiers to be aware of.

I lent out the book to my brother once and he somehow lost it, so I never got it back. Don’t lend out book guys.

And remember not everything can be solved using a regular expression: xkcd.com/1171/

mcepl, (edited ) in Flatpack, appimage, snaps..
@mcepl@lemmy.world avatar

youtu.be/4WuYGcs0t6I (Richard Brown (FOSDEM 2023): “I was wrong about Flatpak, AppImage, and Snap”)

  • All
  • Subscribed
  • Moderated
  • Favorites
  • linux@lemmy.ml
  • localhost
  • All magazines
  • Loading…
    Loading the web debug toolbar…
    Attempt #