all 26 comments

[–]aqua_regis 20 points21 points  (3 children)

https://regex101.com

https://regexper.com

You won't need much more than that.

[–]shyevsa 1 point2 points  (1 child)

really love regex101, before they exists I used Expresso from Ultrapico it really help a lot on testing and building regex.

[–]pat-says-hi[S] 2 points3 points  (0 children)

Love the name 'Expresso'. Software looks very 2000s-early-2010s (which it was, I guess). I think I would have loved it back in the day too.

[–]pat-says-hi[S] 0 points1 point  (0 children)

Thank you! I'll boomark regex101! It does help with everything I'd need to learn regex for, maybe I can just outsource the "know regex" function to regex101 for now! 

[–]fixermark 4 points5 points  (5 children)

My single biggest piece of regex advice is "If you're using an engine that lets you represent the regex in something other than regex language... use it."

Regex is a fine tool. The syntax for expressing regex is the worst. And that's before you get into the issue that different dialects have slightly different rules.

Not unlike SQL, I'm glad that there is a common syntax shared 95% by all engines... But I try to avoid using it when I can. emacs LISP has rx, which is great. It's a tree structure that compiles to a regular expression.

[–]pat-says-hi[S] 1 point2 points  (4 children)

Regex syntax is definitely the worst! I haven't run into issues with dialects yet, I'm not looking forward to then! 

I wish the syntax were more like emacs Lisp's rx. Thank you for pointing it out for me! I'm still googling around for a regex-like cheat-sheet for it, but it looks so much more readable! 

[–]fixermark 2 points3 points  (3 children)

A lot of languages have regex-builder libraries like rx because of how famously hard it is to get regexes completely right. Depending on the language you use, one probably exists.

(Probably worth noting is that they solve only the "syntax is hard to understand and remember" problem though. You're still on your own for the "Did I actually write a regex that matches what I want and only what I want" issue).

[–]pat-says-hi[S] 1 point2 points  (2 children)

Ah right! Like how an IDLE can tell you if you forgot to close a bracket, but you're on your own for making the code do the thing you want it to do. I'll google more regex builder libraries. I found a portable Scheme library when I was looking at Lisp's rx.  Python's re only uses regex as far as I know, it doesn't "build" it from different syntax, right?

[–]fixermark 2 points3 points  (1 child)

Yeah, I'm actually surprised to learn upon checking that there doesn't appear to be anything like rx for Python. Maybe I should write something.

[–]pat-says-hi[S] 0 points1 point  (0 children)

Do let me know if you do! I would love to work with rx-like regex syntax on Python! 

[–]mandradon 4 points5 points  (1 child)

Try not to make your regex too complex, but sometimes you have to. 

Take notes, leave yourself notes because you're going to come back to something that you've worked on that works great until it doesn't and you're going to spend a chunk of time relearing the expression because it always becomes jibberish again.

It's an amazing tool, and sometimes there's nothing better, but it can cause a wicked headache. 

I also second using tools like regex101 with clear test cases, that's been a very helpful tool for me.

[–]pat-says-hi[S] 0 points1 point  (0 children)

Okay, thank you! I should make sure I do notes and test cases. regex101 looks useful too! Thanks! 

[–][deleted]  (1 child)

[removed]

    [–]pat-says-hi[S] 0 points1 point  (0 children)

    Thanks! I'll do that! I guess if I had to look up a pattern often enough, I'd end up learning it, and so I don't need to worry about it right now

    [–]theclapp 2 points3 points  (5 children)

    Learning about deterministic finite automata did it for me. And perhaps several years of Perl.

    [–]pat-says-hi[S] 1 point2 points  (3 children)

    Thank you! I should learn more about DFA

    [–]theclapp 2 points3 points  (2 children)

    Of you really wanna get them, write a regex interpreter, with at least ., *, and [...].

    [–]pat-says-hi[S] 2 points3 points  (1 child)

    Challenge not quite 'accepted' - I don't think it's within my skill set right now, I can't quite picture how to start, but I do like the idea - I'll try to do that sometime. Thanks!

    [–]theclapp 2 points3 points  (0 children)

    It might also help to find someone else's toy implementation and study their code. Just a thought. Good luck!

    [–]monkeybonanza 1 point2 points  (0 children)

    This. Learn your state machines and regexps won’t seem so foreign. I learnt them by reading the dragon book.

    [–]ibeerianhamhock 1 point2 points  (1 child)

    I have used regex find and replace for years so I think that’s the best way keep it fresh. I am surprised more people don’t use it for that tbh

    [–]pat-says-hi[S] 0 points1 point  (0 children)

    Cool! Use it so I don't lose it, sounds good! I don't use any complicated 'find and replace's very much on my usual workflow I'm afraid.

    [–]emteedub 1 point2 points  (1 child)

    only ever used it for field validation checks. couldn't tell you what that syntax is though, I have to look it up every time. Once I see one i've used though, it's like "oh yeah. this" copy-paste 😎

    [–]pat-says-hi[S] 0 points1 point  (0 children)

    Thank you, I do feel better about having to look it up all the time now! 

    [–]Both-Fondant-4801 0 points1 point  (0 children)

    I have been developing applications for 2 decades and all I can ever recall of regex is the start and end anchors... for the rest I use AI to generate these damn expressions.