News for hackers. Stuff that matters.
Re:Regular Expressions make me feel like a powerful wizard- ... by puffoflogic on Thursday February 09, 2023 @11:06PM (#34735484)
Even this is unnecessary. I'd say 99% of people using PCRE-like regexes have never read the documentation and realized `(?x)` exists.

We can write the regex like this, including all whitespace:

    (?x)
    ^\w+            # mandatory leading letters
    ( [-+.'] \w+ )* # optional suffix
    @
    \w+             # domain
    ( [-.] \w+ )*   # domain suffix
    ( \.\w+ ( [-.] \w+ )* )* #tld
    $
Also, btw, I hope no one is really using this regex. It's wrong; for example it appears to be deliberately designed to fail on IDNs.
Re:Regular Expressions make me feel like a powerful wizard- ... by goguy on Friday February 10, 2023 @01:31AM (#34736345)
That's less readable than the original suggestion though.
Re:Regular Expressions make me feel like a powerful wizard- ... by sundarurfriend on Friday February 10, 2023 @01:56AM (#34736518)
Out of context on HN, it looks so. But in my experience, the original suggestion with the separate variables gets pretty hard to keep track of, and ends up making things more confusing. The `(?x)` or `/.../x` version makes it much easier to see things in their place and understand the overall pattern.
Re:Regular Expressions make me feel like a powerful wizard- ... by RheingoldRiver on Friday February 10, 2023 @03:01PM (#34744963)
That's pretty neat, indeed I'd never heard of this, or maybe I had once but I'd forgotten about it if so. I might counter that in a very long regex you'd want to do the assembly in multiple steps, but I can definitely see the appeal of inline comments (perhaps in addition to something like that). I'll try to keep this in mind for the next time I'm dealing with regex. Thanks for sharing!
Re:Regular Expressions make me feel like a powerful wizard- ... by atlantic on Friday February 10, 2023 @07:05AM (#34738600)
Outstanding method for commenting regexes. I had no idea about (?x). Thanks for this - I'll adopt it going forwards.