Comments in Regex in PHP

The modifier x allows placing arbitrary spaces and line breaks in the regex. Moreover, the hash symbol becomes a single-line comment symbol (accordingly, the delimiters will have to be replaced with others).

Let's look at an example. Suppose we have the following regex:

php<?php
  preg_replace('#[a-z]+@[0-9]+#', '!', 'aaa@333');
?>

Let's use the modifier x and add explanatory comments for each part of our regex:

php<?php
  preg_replace('~
    [a-z]+ # letters
    @      # at symbol
    [0-9]+ # digits
  ~x', '!', 'aaa@333');
?>