PHP Regex Delimiter, What's the Point

PHP regex delimiter, what's the point?

There is no technical reason why it has to be like this.

As noted in a comment, the underlying library does not require that flags be passed as part of the regexp - in fact, the extension has to strip these off and pass them as a separate argument.

It appears as though the original implementer was trying to make it look like grep/sed/awk/perl/etc so that it is more familiar to programmers coming from those tools.

Purpose of Delimeters in preg_match()

Delimiters are used to wrap your regular expression. It tells the interpreter which characters make up your regular expression and which characters are relevant flags to modify how the interpreter acts on your regular expression. Other than that, it is simply the way it was implemented.

PHP Regex / delimiter

Just use explode():

$result = array_filter(explode('/', $string));

array_filter() removes the empties from the / on either end. Alternately you could trim() it:

$result = explode('/', trim($string, '/'));

But to answer the question, you would just use / as the pattern for preg_split() and either escape the / as in /\// or use different delimiters:

$result = array_filter(preg_split('#/#', $string));

Another way depending on your needs and complexity of the string contents:

preg_match_all('#/([^/]+)#', $string, $result);
print_r($result[1]);

$result[0] is an array of full matches and $result[1] is an array of the first capture group (). If there were more capture groups you would have more array elements in $result.

PHP Regex delimiter

One thing that needs correcting is that if your regular expression and/or input data is encoded in UTF-8 (which in this case it is, since it comes straight from inside a UTF-8 encoded file) you must use the u modifier for your regular expression.

Another issue is that the copyright character should not be used as a delimiter in UTF-8 because the PCRE functions consider that the first byte of your pattern encodes your delimiter (this could plausibly be called a bug in PHP).

When you attempt to use the copyright sign as a delimiter in UTF-8, what actually gets saved into the file is the byte sequence 0xC2 0xA9. preg_match looks at the first byte 0xC2 and decides that it is an alphanumeric character because in your current locale that byte corresponds to the character Latin capital letter A with circumflex  (see extended ASCII table). Therefore a warning is generated and processing is immediately aborted.

Given these facts, the ideal solution would be to choose an unusual delimiter from inside the ASCII character set because that character would encode to the same byte sequence both in single byte encodings and in UTF-8.

I would not consider printable ASCII characters unusual enough for this purpose, so a good choice would be one of the control characters (ASCII codes 1 to 31). For example, STX (\x02) would fit the bill.

Together with the u regex modifier this means you should write the regex like this:

$result = preg_match("\x02<.*?>\x02u", '<something string>');

Why does the delimiter used affect the validity of a regex?

You were using double quotes:

 "~^[\x20-\x7E]+$~"

Which means that both \x20 and \x7E got interpreted in PHP string context, not by PCRE. Guess what \x7E amounts to.

So as @Bitwise mentioned, use single quotes. Or better yet escape the escape sequences:

 "~^[\\x20-\\x7E]+$~"

Thus the regex engine will still see [\x20-\x7E] instead of [ -~].

PHP regex delimiters, / vs. | vs. {} , what are the differences?

No difference, except the closing delimiter cannot appear without escaping.

This is useful when the standard delimiter is used a lot, e.g. instead of

preg_match("/^http:\\/\\/.+/", $str);

you can write

preg_match("[^http://.+]", $str);

to avoid needing to escape the /.

Regex matching between # delimiters

Several ways to do it:

  1. s modifier for . to match newlines
  2. \\\\ to escape the \
  3. There is probably a \r\n instead of just a \n so I used \s

 preg_match("/#\sa\s(.*)\s#\s\\\\a/s", $str, $matches);

Why in the world do you use {1} everywhere?

PHP Regex: Matching Space if only within delimiter

You may use

(?:\G(?!\A)|\[)[^]\s]*\K\s+

See the regex demo

Details:

  • (?:\G(?!\A)|\[) - the end of the previous successful match (\G(?!\A)) or (|) the [ symbol
  • [^]\s]* - 0 or more chars other than ] and whitespace
  • \K - match reset operator omitting all text matched so far in the current iteration
  • \s+ - 1+ whitespaces

PHP demo:

$str = '[Edit the] Expression & [Text to see matches]. Roll [over matches or the] expression for details. Undo mistakes with ctrl-z. [Save Favorites and Share expressions with friends] or the Community. Explore your results with Tools. [A full Reference and Help is available in the Library or watch the video Tutorial].';
$result = preg_replace('~(?:\G(?!\A)|\[)[^]\s]*\K\s+~', '$', $str);
echo $result;

Another approach: match [...] substrings (with the \[[^][]+] pattern) and only replace whitespace inside the matches with preg_replace_callback:

$result = preg_replace_callback('~\[[^][]+]~', function ($m) {
return preg_replace('~\s+~', '$', $m[0]);
}, $str);

See another PHP demo.

Regex extract characters between two string including delimiters

You may extract those substrings between ( and ) using

preg_match('~.*\K\([^()]*\)~s', $s, $matches)

See the regex demo.

Details

  • .* - any 0+ chars, as many as possible
  • \K - match reset operator that discards the text matched so far from the match buffer
  • \( - a ( char
  • [^()]* - 0+ chars other than ( and )
  • \) - a ) char.


Related Topics



Leave a reply



Submit