Named Captures: The Art of Naming Things

When it comes to regular expressions, naming your captures can be a delicate matter. A well-chosen name can make your code more readable, more maintainable, and more less likely to give you grey hair.

Why Name Your Captures?

Let's face it, folks: regular expressions can get complex. Named captures make it easier to understand what's going on, especially when you come back to a code after a year or two.

Capture Naming Conventions

Here are some general guidelines for naming your captures:

Some people like to use abbreviations for their captures, but that's a whole other can of worms.

Examples

Here are some examples of well-named captures:

			(?<capture_name>[^@]+)
		

And here's a bad example:

			/(?<capture_name>[^@]+)/
		

Best Practices

When naming your captures, keep in mind:

Learn about abbreviations