Live data from Hacker News

Show HN: Regex Cheatsheet

ihateregex.io

111–120 of 135 posts

Re: Show HN: Regex Cheatsheet

#112

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…

RE2 syntax[1] is a pretty good option to learn, because it's mostly a "lowest common denominator" - if it works in RE2, it should work in PCRE, Python, Javascript, etc. The reverse isn't true - there is a bunch of syntax that RE2 doesn't support by design, often to constrain performance bounds. Emacs regexps are unfortunately their own weird beast - they handle parentheses differently than other regexp engines, becau…

IME Emacs provides a very pleasant way to write regexps using the rx library. ELPA also has the package xr, which converts Elisp regexps to rx format, and pcre2el converts PCRE to Elisp. So a regexp like

    \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
Can easily be converted like:

    (->> "\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\\b"
         pcre-to-elisp xr)
To:

    (seq word-boundary
         (one-or-more
          (any "0-9A-Z" "%+._-"))
         "@"
         (one-or-more
          (any "0-9A-Z" ".-"))
         not-newline
         (repeat 2 4
                 (any "A-Z"))
         word-boundary)

Re: Show HN: Regex Cheatsheet

#113

Earlier quoted context omitted.

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…

A couple of things going on here. First, the "~" characters aren't really part of the regular expression. As far as I can tell, they are delimeters to mark the start/stop of this. Often you will see "/" used for this purpose. Next is: \( ... \) This matches a pattern that starts with the literal character '(' and ends with ')', where what comes between them matches the elided portion. Since parantheses have special m…

Wow thanks for explaining it to me so wonderfully. Your explanation for the double ++ really helped me since that part never made sense to me before. I guess the ?R probably only works with PHP? I will try to make some more examples for the ?R to try out today so I can learn the full power of it.

Again I'm so grateful to you for the explanation. One more thing I've learned from it is next time a regex makes my head explode, I'll just break each character in one line and write a comment next to it!

Re: Show HN: Regex Cheatsheet

#115

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…

To me, the divide is pre and post-Perl.

It's not so bad going between JS, Ruby and Elixir regex (possibly due to my use of a smaller set of features), but VIM regex disappoint me time after time.

Re: Show HN: Regex Cheatsheet

#116
post #90

Earlier quoted context omitted.

> Honestly, as a noob, this is one of the biggest reasons I have such a hard time deciding to learn regex. Clear your afternoon, and just learn it. Seriously, it takes a couple of hours at best and then - BOOM - you're done for the rest of your life.

If you believe it is possible to become an expert in regular expressions as they exist in modern computer languages in "a couple of hours at best" you are delusional.

He didn't say you could become an expert in a single afternoon.

Re: Show HN: Regex Cheatsheet

#117
post #90

Earlier quoted context omitted.

> Honestly, as a noob, this is one of the biggest reasons I have such a hard time deciding to learn regex. Clear your afternoon, and just learn it. Seriously, it takes a couple of hours at best and then - BOOM - you're done for the rest of your life.

> you're done for the rest of your life. If that were so easy then I don't think much of these cheatsheets would exist.

The basic regex is easy, infact an English word is a regex! A dot matches a single character. Star multiple of the previous character. Just that is useful for a lot of cases!

Re: Show HN: Regex Cheatsheet

#118
For the love of god, PLEASE DON’T USE REGEX TO VALIDATE EMAIL. The RegEx of this website ignores plus-addressing, for example. All you need to do to validate email is send a verification email.

Re: Show HN: Regex Cheatsheet

#119
post #118

For the love of god, PLEASE DON’T USE REGEX TO VALIDATE EMAIL. The RegEx of this website ignores plus-addressing, for example. All you need to do to validate email is send a verification email.

not just the email regex is simplified (and at the end plain wrong). Also the one for phone numbers is highly simplified and will not match all valid phone numbers...

Re: Show HN: Regex Cheatsheet

#120
post #39

Earlier quoted context omitted.

Even that is wrong because you can have privately owned TLDs (I forget what they're technically called) like .google So sundar.pichai@google is technically a valid address (whether .google has any MX records is another matter) Regex shouldn't really be used for email addresses anyway because the only reliable way to authenticate an email address is to literally send an email to that address.

.google does not have any MX records

No, but theoretically it could.
Post reply on HN