4

This is bugging me:

Why preg_match('/pattern/', $haystack) instead of preg_match('pattern', $haystack)? Everything I've seen just states as a fact that they're necessary, and mentions that you can use alternate delimiters.

But, it's a function that defines its own interface outside of the string. It has a flags argument. Adding intra-string syntax seems capricious.

Is it something inherited from pcre that the authors were just not interested in working around? Yet another perverse fact of PHP? Or is there a justification?

10
  • 1
    Why should they make delimiters optional? They are Perl-compatible regular expressions after all. Also, the $flags argument in PHP's PCRE functions is not where you put modifiers, unlike the flags argument in JavaScript's RegExp() constructor. Commented Mar 3, 2012 at 23:50
  • Lost in the history of PHP development. Maybe the Chinese philosopher, F**k, knows. Maybe its because the authors didn't want you to use a separate arg for the flag modifiers. Commented Mar 4, 2012 at 0:05
  • @BoltClock: My question is why do they exist at all? The delimiters are part of Perl's syntax, they delimit the regex literal. The PHP regex functions take a string, which already has a perfectly good delimiter in the 's that appear at the beginning and end of the string. And I know that the $flags isn't used for flags. That's just dumb. Commented Mar 4, 2012 at 0:07
  • If you really must know why, please ask the original author, not SO. It doesn't sound like you'd be satisfied with anyone else's response (and likely not even theirs). Commented Mar 4, 2012 at 0:09
  • @quodlibetor If you are really bothered by the delimiters and the implementation, you can implement your own my_preg_match() which accepts a regex without delimiters and adds them, wrapping preg_match() internally. Commented Mar 4, 2012 at 0:10

4 Answers 4

6

The delimiters are for compatibility with Perl. Perl regular expressions use the delimiters, and rely on the end delimiter to signify the start of modifier flags, like i for case insensitivity, for example.

// Match alpha-numeric, case insensitive, multiline
preg_match('/^[a-z0-9]+$/im', $input);

The optional flags argument to preg_match() does not implement the regular expression flags like i that follow the second delimiter. They serve a different function, and indeed PREG_OFFSET_CAPTURE is the only flag available there. This is not to say the regex flags could not have been implemented as another function parameter. They certainly could have, but Perl-compatibility is the goal here.

PHP is not the only language that borrows directly from Perl to implement regular expressions. JavaScript does to some degree, and Ruby even implements Perl's =~ operator for regular expression matches.

Sign up to request clarification or add additional context in comments.

4 Comments

This might be true, but "For compatibility with Perl" doesn't really make sense: the delimiters are a syntactic feature of Perl, not PHP. Flags could very easily have been put into the already existing flags argument of the preg_* functions. The shallow effects of delimiters on the syntax don't affect compatibility on anything like a deep level.
@quodlibetor PHP is not the only language that implements PCRE with delimiters. Ruby does too, and JavaScript to some degree as well. Could they be implemented differently? Sure could. Does it make sense to implement them differently? Not sure about that...
PHP is the only language that puts the delimiters inside of strings. Which use ' or "" as their delimiters. Ruby and JS both took Perl's syntax for regular expressions: You don't write '/pattern/' in them, you write /pattern/. It's a language feature, not a function, in other languages.
@quodlibetor That is true. PHP is in general, a language awash in inconsistencies legacy architectural oddities.
0

I believe it was done for interoperability, so you can copy&paste pattern from/to another language and not having to convert pattern options to/from PHP flags.

Comments

0

The delimiter is for Perl compatibility (thus, Perl Compatible Regular Expressions, or PCRE). The pattern also includes any modifiers you choose to use, and they must be placed after the closing delimiter.

Comments

-1

Is it something inherited?

The truth is every programming language needs a standard so PCRE is a better standard. click

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.