3 ms·
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 li
by puffoflogic 4y ago
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.
- goguy 4y agoThat's less readable than the original suggestion though.
- sundarurfriend 4y agoOut 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.
- RheingoldRiver 4y agoThat'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!
- atlantic 4y agoOutstanding method for commenting regexes. I had no idea about (?x). Thanks for this - I'll adopt it going forwards.