0

I have seen some examples about regular expressions with "@" on PHP, something like this:

preg_match ("@[a-zA-Z0-9]@i", $value, $occurrences);

I could see that this counts the number of matches of regular expression which were found in the $value variable, but I would like to be sure if the "@" is used for this specific case or what's the main function of "@" in regular expressions?.

Can you help me please?

hmrojas.p
  • 562
  • 1
  • 5
  • 15
  • that present `@` is only a delimiter. It could have been `#` or `/` if that's what the question is about. RTM http://php.net/manual/en/function.preg-match.php – Funk Forty Niner May 05 '16 at 16:26
  • 1
    Delimiter secrets http://stackoverflow.com/a/37058367/557597 –  May 05 '16 at 19:15

1 Answers1

7

These are custom delimiters for the regular expression pattern. The most common is /, in your case they were changed to @.

The benefit of custom delimiters has to do with escaping. This is best shown by example.

Consider:

preg_match ("/Some\/Path/i", $value, $occurrences);

Versus:

preg_match ("#Some/Path#i", $value, $occurrences);

However, on a personal note, I tend to avoid them as it makes the regular expression pattern, well, customized. Just use the standard / delimiters.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • 1
    Using a metacharacter as a delimiters is a problem, you should mention that. –  May 05 '16 at 16:34
  • the option requiring less escapes might end up being more portable though eh? in c#, for example, the # delimited regex with no escaped / would be the valid one. – Scott Weaver May 05 '16 at 16:40
  • 1
    Delimiters are a PCRE thing, and you can use whatever you want for them in anything that implements PCRE. The only "portability" problem is moving the regex to not-PCRE where it still doesn't matter what the delimiter is because you have to remove them altogether. – Sammitch May 05 '16 at 17:07
  • 1
    @Jason McCreary thanks for your help. – hmrojas.p May 05 '16 at 17:45
  • Thanks for the clarification @Sammitch. I have updated my note. – Jason McCreary May 05 '16 at 20:52