Live data from Hacker News

Show HN: Regex Cheatsheet

ihateregex.io

71–80 of 135 posts

Re: Show HN: Regex Cheatsheet

#71

OK, these kinds of regex tools get posted quite often. I get it, regex is very confusing at first. And some of these use-cases result in rather complex expressions nobody should be forced to write from scratch (you are still remembering to write unit tests for them though, right?) But as someone who actually knows [some flavours of] regex fairly well, what I would really like, is a reference that covers all the subtl…

regex101 does a good job at showing you what the selected variant can do.

Re: Show HN: Regex Cheatsheet

#72

Regex are quite simple and useful but my only issue is with those recursive things. Like how do you match balanced brackets? I have a regex (pcre) copy-pasted for it but for the life of me I don't get it or maybe nod my head but instantly ununderstand it. I wish there was a simple to understand doc that teaches to me how I can match something like: "(this is inside a bracket (and this is nested or (double nested))) P…

Can you explain the problem further?

Re: Show HN: Regex Cheatsheet

#73
post #53

I use regex a lot but deliberately keep it simple. One thing that confounded me often was positive and negative look-arounds. I always got the expressions mixed up, until I just put the expressions into a table like this... look-behind | look-ahead ------------------------------------ positive (? It's not hard, but for whatever reason my brain had trouble remembering the usage because every time I looked it up, each…

Maybe it's easier to remember that lookbehinds are evil from an implementation standpoint, and even in Perl have arbitrary limitations. If you see lookbehinds, look away! If you see lookaheads, go ahead.

It's something I really like about .NET's regular expressions. Lookbehind has no limitations and will just match backwards with all features you can use in other parts.

So depending on the language or flavor you're working in, running away isn't really necessary.

Re: Show HN: Regex Cheatsheet

#74
post #35
post #34

Earlier quoted context omitted.

My go-to is https://regex101.com/

Didn't know about this. Thanks!

We use it on slack and irc for debugging people's regular expressions all the time. Being able to have 30 revisions to a base regex to troubleshoot is fantastic.

Plus the quiz is awesome.

Re: Show HN: Regex Cheatsheet

#75
post #4

I love regex and have no trouble reading them, but still love this tool, great job. I especially like the railroad diagrams, for those cases where I brainfarted on a regex and it's doing something other than what I intended. Thanks for this.

I'm glad you like the tool <3 It will have a lot more content soon :)

If you want some help swing by #regex on efnet, happy to help.

Re: Show HN: Regex Cheatsheet

#76

Regex are quite simple and useful but my only issue is with those recursive things. Like how do you match balanced brackets? I have a regex (pcre) copy-pasted for it but for the life of me I don't get it or maybe nod my head but instantly ununderstand it. I wish there was a simple to understand doc that teaches to me how I can match something like: "(this is inside a bracket (and this is nested or (double nested))) P…

Balanced paranthesis are not a regular language, so it s theoretically imposdible to match them with regular expressions.

In practice, most regexp implemenations you see are more powerful then regular expressions. For instance, .net has a balancing groups feature [0] for exactly this usecase.

[0] https://regular-expressions.mobi/balancing.html?wlr=1

Re: Show HN: Regex Cheatsheet

#77

OK, these kinds of regex tools get posted quite often. I get it, regex is very confusing at first. And some of these use-cases result in rather complex expressions nobody should be forced to write from scratch (you are still remembering to write unit tests for them though, right?) But as someone who actually knows [some flavours of] regex fairly well, what I would really like, is a reference that covers all the subtl…

I tend to go to https://www.regular-expressions.info when I need to find out which features are supported between dialects. Not always up-to-date, but has some good info.

Re: Show HN: Regex Cheatsheet

#78

Regex are quite simple and useful but my only issue is with those recursive things. Like how do you match balanced brackets? I have a regex (pcre) copy-pasted for it but for the life of me I don't get it or maybe nod my head but instantly ununderstand it. I wish there was a simple to understand doc that teaches to me how I can match something like: "(this is inside a bracket (and this is nested or (double nested))) P…

Balanced paranthesis are not a regular language, so it s theoretically imposdible to match them with regular expressions. In practice, most regexp implemenations you see are more powerful then regular expressions. For instance, .net has a balancing groups feature [0] for exactly this usecase. [0] https://regular-expressions.mobi/balancing.html?wlr=1

The regex I've copy-pasted is this:

    $str = "(this is inside a bracket (and this is nested or (double nested)))";
    do {
        preg_match_all('~\(((?:[^\(\)]++|(?R))*)\)~', $str, $matches);
        echo $str = $matches[1][0] ?? '', "\n";
    } while($str);
Outputs this [1]:

    > this is inside a bracket (and this is nested or (double nested))
    > and this is nested or (double nested)
    > double nested

You're right that there is more processing involved (e.g. while loop) but I still don't understand this part

    '~\(((?:[^\(\)]++|(?R))*)\)~'
[1] https://rextester.com/MEH86820

Re: Show HN: Regex Cheatsheet

#79
Either I'm a regex wizard and don't know it, or perhaps I think I know something but know nothing at all but I've never complained about using regex expressions. I use them all the time without thought. Never quite figured out the need for a cheatsheet either, your language of choice should have a good documentation page for any specific supported syntax.

Re: Show HN: Regex Cheatsheet

#80
post #72

Regex are quite simple and useful but my only issue is with those recursive things. Like how do you match balanced brackets? I have a regex (pcre) copy-pasted for it but for the life of me I don't get it or maybe nod my head but instantly ununderstand it. I wish there was a simple to understand doc that teaches to me how I can match something like: "(this is inside a bracket (and this is nested or (double nested))) P…

Can you explain the problem further?

please see my reply to @gizmo686
Post reply on HN