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…
Show HN: Regex Cheatsheet
71–80 of 135 posts
Re: Show HN: Regex Cheatsheet
#72Regex 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…
Re: Show HN: Regex Cheatsheet
#73I 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.
So depending on the language or flavor you're working in, running away isn't really necessary.
Re: Show HN: Regex Cheatsheet
#74Earlier quoted context omitted.
My go-to is https://regex101.com/
Didn't know about this. Thanks!
Plus the quiz is awesome.
Re: Show HN: Regex Cheatsheet
#75I 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 :)
Re: Show HN: Regex Cheatsheet
#76Regex 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…
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.
Re: Show HN: Regex Cheatsheet
#77OK, 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…
Re: Show HN: Regex Cheatsheet
#78Regex 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
$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/MEH86820Re: Show HN: Regex Cheatsheet
#79Re: Show HN: Regex Cheatsheet
#80Regex 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?