Reputation: 4608
I found this regex that works correctly but I didn't understand what is #
(at the start) and at the end of the expression. Are not ^
and $
the start/end characters?
preg_match_all('#^/([^/]+)/([^/]+)/$#', $s, $matches);
Thanks
Upvotes: 1
Views: 101
Reputation: 7946
Let me break it down:
#
is the first character, so this is the character used as the delimiter of the regular expression - we know we've got to the end when we reach the next (unescaped) one of these
^
outside of a character class, this means the beginning of the string
/
is just a normal 'slash' character
([^/]+)
This is a bracketed expression containing at least one (+
) instance of any character that isn't a / (^
at the beginning of a character class inverts the character class - meaning it will only match characters that are not in this list)
/
again
([^/]+)
again
/
again
$
this matches the end of the string
#
this is the final delimeter, so we know that the regex is now finished.
Upvotes: 0
Reputation: 15411
The matched pattern contains many /
, thus the #
is used as regex delimeter. These are identical
/^something$/
and
#^something$#
If you have multiple /
in your pattern the 2nd example is better suited to avoid ugly masking with \/
. This is how the RE would like like with using the standard //
syntax:
/^\/([^\/]+)\/([^\/]+)\/$/
Upvotes: 3
Reputation: 324750
You can use pretty much anything as delimiters. The most common one is /.../
, but if the pattern itself contains /
and you don't want to escape any and all occurrences, you can use a different delimiter. My personal preference is (...)
because it reminds me that $0
of the result is the entire pattern. But you can do anything, <...>
, #...#
, %...%
, {...}
... well, almost anything. I don't know exactly what the requirements are, but I think it's "any non-alphanumeric character".
Upvotes: 0
Reputation: 23690
These are delimiters. You can use any delimiter you want, but they must appear at the start and end of the regular expression.
Please see this documentation for a detail insight in to regular expressions: http://www.php.net/manual/en/pcre.pattern.php
Upvotes: 0
Reputation: 198119
About #
:
That's a delimiter of the regular expression itself. It's only meaning is to tell which delimiter is used for the expression. Commonly /
is used, but others are possible. PCRE expressions need a delimiter with preg_match
or preg_match_all
.
About ^
:
Inside character classes ([...]
), the ^
has the meaning of not if it's the first character.
[abc] : matching a, b or c
[^abc] : NOT matching a, b or c, match every other character instead
Upvotes: 1
Reputation: 26930
Also # at the start and the end here are custom regex delimiters. Instead of the usual /.../
you have #...#
. Just like perl.
Upvotes: 0