DEV Community

Cover image for Regex That Actually Works (Copy-Paste Ready Examples)
Andrew Rozumny
Andrew Rozumny

Posted on

Regex That Actually Works (Copy-Paste Ready Examples)

You paste a regex.

It doesn’t work.

You change one character.

Now it works.

You have no idea why.


At some point you just accept it.

Regex is not something you fully understand.

It’s something you negotiate with.


Typical workflow:

  • google "regex email"
  • copy something scary-looking
  • paste
  • test
  • slightly panic
  • tweak random symbol
  • it works
  • never touch it again

Here are a few patterns that actually come up all the time.

Email (good enough, not perfect):

^[^\s@]+@[^\s@]+\.[^\s@]+$
Enter fullscreen mode Exit fullscreen mode

URL (basic, but works in most cases):

https?:\/\/[^\s]+
Enter fullscreen mode Exit fullscreen mode

Only numbers:

^\d+$
Enter fullscreen mode Exit fullscreen mode

Remove extra spaces:

\s+
Enter fullscreen mode Exit fullscreen mode

The problem with regex is not writing it.

It’s understanding it later.

You come back after a week and think:

“what is this even matching?”


Most bugs are stupid:

  • forgot to escape something
  • used greedy match by accident
  • missed boundaries

Nothing complex.

Just painful.


What helped me:

stop trying to be clever.

Start simple. Then expand.


Also, use a tester.

Seriously.

👉 https://tooldock.org/tools/regex-tester

Paste → tweak → see what’s happening.

Way faster than guessing.


If you have one regex you keep reusing — drop it below.

I’m pretty sure we’re all sharing the same 5 patterns anyway.

Top comments (0)