<!--
title: Optimize regex patterns
domain: observability
topic: Algorithms
language: PHP
source: getsentry/sentry-php
updated: 2022-05-15
url: https://awesomereviewers.com/reviewers/sentry-php-optimize-regex-patterns/
-->

When using regular expressions, optimize for both performance and precision to ensure efficient and accurate pattern matching:

1. **Use non-capturing groups** when you don't need to reference the matched content, reducing memory allocations:
   ```php
   // Less efficient
   preg_replace('/0x[a-fA-F0-9]+$/', '', $string);
   
   // More efficient
   preg_replace('/(?:0x)[a-fA-F0-9]+$/', '', $string);
   ```

2. **Use strict character classes** for specific formats:
   ```php
   // Too permissive for hex format
   preg_replace('/0x[a-zA-Z0-9]+$/', '', $string);
   
   // Correct for hex format
   preg_replace('/0x[a-fA-F0-9]+$/', '', $string);
   ```

3. **Use anchors and escape special characters** when matching exact patterns:
   ```php
   // Without anchors - could match substrings
   expect($error)->toMatch('/The option "foo" does not exist./');
   
   // With anchors and escaped special characters - matches whole string precisely
   expect($error)->toMatch('/^The option "foo" does not exist\.$/')
   ```

These optimizations lead to more predictable pattern matching, fewer bugs in data processing, and better performance when dealing with large datasets.
