Regex is a pain.

Don& #39;t force people to learn it every time they read your code.

Here& #39;s how I regex with empathy:
So much regex passion! You love to see it. Tech twitter doesn’t disappoint.

Attempting to respond to frequent replies below. https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
https://abs.twimg.com/emoji/v2/... draggable="false" alt="1️⃣" title="Tastenkappe Ziffer 1" aria-label="Emoji: Tastenkappe Ziffer 1"> “Just learn regex.”

I haven’t met anybody that’s learned regex once and remembered it forever. I find I have to re-teach myself regex every time I see it.

This example is simple, yes. But regex gets complex.

Perhaps your memory is better than mine. More power to you. https://abs.twimg.com/emoji/v2/... draggable="false" alt="😄" title="Lächelndes Gesicht mit geöffnetem Mund und lächelnden Augen" aria-label="Emoji: Lächelndes Gesicht mit geöffnetem Mund und lächelnden Augen">
https://abs.twimg.com/emoji/v2/... draggable="false" alt="2️⃣" title="Tastenkappe Ziffer 2" aria-label="Emoji: Tastenkappe Ziffer 2"> “Just permalink it to regex101.”

Be cautious with this. Your company might have strict policies about sharing confidential source code with a third party. Even for a small regex.

The example here is common enough, but other regular expressions are sensitive.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="3️⃣" title="Tastenkappe Ziffer 3" aria-label="Emoji: Tastenkappe Ziffer 3"> “Readers can paste it to an online regex tool.”

Fair. I& #39;d recommend linking the specific tool in the source code. Some are better than others.

Again, be cautious. Though unlikely, it’s within that tool& #39;s power to start POST-ing the regex back to their servers.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="4️⃣" title="Tastenkappe Ziffer 4" aria-label="Emoji: Tastenkappe Ziffer 4"> “Comments are code smells. Just use a better function name.”

This is a simple example. For other regex you might have trouble coming up with a name.

Yes, there’s a risk the code can be changed without the comment. It’s a tradeoff.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="5️⃣" title="Tastenkappe Ziffer 5" aria-label="Emoji: Tastenkappe Ziffer 5"> “Unit tests are better than documentation.”

You should unit test. Unit tests and documentation need not be mutually exclusive.

But using regex often results in a large number of tests to cover edge cases. It& #39;s difficult to deduce what the regex does with unit tests alone.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="6️⃣" title="Tastenkappe Ziffer 6" aria-label="Emoji: Tastenkappe Ziffer 6"> "& #39;Match every character& #39; is misleading.”

I agree. The comment is intended to describe the function of ^ and $ within the expression, not their literal definition in regex.

By definition those match the start and end of the string. The comment can be improved.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="7️⃣" title="Tastenkappe Ziffer 7" aria-label="Emoji: Tastenkappe Ziffer 7"> “I’m going to attack you personally because of the way you’re documenting regex.”

Relax. It’s just code. There are tradeoffs and this won’t work for everyone in all use cases.

Here’s a picture of me to cheer you up. https://abs.twimg.com/emoji/v2/... draggable="false" alt="😄" title="Lächelndes Gesicht mit geöffnetem Mund und lächelnden Augen" aria-label="Emoji: Lächelndes Gesicht mit geöffnetem Mund und lächelnden Augen">
https://abs.twimg.com/emoji/v2/... draggable= “I’m going to attack you personally because of the way you’re documenting regex.”Relax. It’s just code. There are tradeoffs and this won’t work for everyone in all use cases.Here’s a picture of me to cheer you up. https://abs.twimg.com/emoji/v2/... draggable="false" alt="😄" title="Lächelndes Gesicht mit geöffnetem Mund und lächelnden Augen" aria-label="Emoji: Lächelndes Gesicht mit geöffnetem Mund und lächelnden Augen">" title="https://abs.twimg.com/emoji/v2/... draggable="false" alt="7️⃣" title="Tastenkappe Ziffer 7" aria-label="Emoji: Tastenkappe Ziffer 7"> “I’m going to attack you personally because of the way you’re documenting regex.”Relax. It’s just code. There are tradeoffs and this won’t work for everyone in all use cases.Here’s a picture of me to cheer you up. https://abs.twimg.com/emoji/v2/... draggable="false" alt="😄" title="Lächelndes Gesicht mit geöffnetem Mund und lächelnden Augen" aria-label="Emoji: Lächelndes Gesicht mit geöffnetem Mund und lächelnden Augen">" class="img-responsive" style="max-width:100%;"/>
You can follow @curtiseinsmann.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: