What does the "~" character signify in PHP regex?
Asked Answered
I

3

5

What does the "~" character mean in the following?:

preg_match_all("~<img [^>]+>~", $inputw, $output);

My guess is that they are beginning and end markers such as ^ and $.

Islamize answered 7/2, 2014 at 9:57 Comment(2)
My guess is that they are beginning and end markers such as ^ and $. Seems like you answered your own question.Carlie
@ShankarDamodaran, except that that's not the case - they've got nothing to do with ^ and $...Stockmon
F
8

It is a delimiter

A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

Fossorial answered 7/2, 2014 at 9:59 Comment(0)
T
4

As Nambi said you are free to choose the delimiter in a regex. However if the delimiter appears in the pattern it has to escaped. Knowing this, imagine the following situation

'/\/var\/www\/test/' # delimited with /
'~/var/www/test~' # delimited with ~

The last one does not require to escape the / as the delimiter is now ~. Much cleaner isn't it?

As a general guideline you are encouraged to choose a delimiter which isn't pattern of the pattern itself, I guess ~ is widely distributed as an alternative to / as it rarely appears in real world pattern.

Tautomer answered 7/2, 2014 at 10:2 Comment(7)
@Nambinarayanan's link says to that: If the delimiter appears often inside the pattern, it is a good idea to choose another delimiter in order to increase readability. ;)Pragmatics
@dollarvar You know what they say about linksStockmon
@dollarvar that it is better to include essential parts of the answer here on SO. I figured you were implying this answer is redundant because the link provided already says that, but I may well have been mistaken.Stockmon
@dollarvar I'm with OGHaza here (otherwise I wouldn't gave the answer). I felt like it is ok to say 1 or 2 additional sentences here.Tautomer
@Stockmon Ah, no, have to take two things out of your comment: a) I may be confused, but do you say, the other answerer did not include enough? b) NO, I just gave the content of the link, because it FITTED and well supported the answer, but then he added more, and then it seemed, well redundant to post that, lol, should I delete the comment?Pragmatics
@dollarvar It took me a while to get the intention of your comment.. no, it's fine :)Tautomer
I see, well it was twisted I guess perhaps neither he nor me (just) stated that it was the manual. :)Pragmatics
E
0

The dirty little delimiter secret they don't tell you ->
http://uk.php.net/manual/en/regexp.reference.delimiters.php

Examples:

Paired delimiters (raw: \d{2}Some\{33\}\w{5})

{\d{2}Some\\{33\\}\w{5}} parses to \d{2}Some\\{33\\}\w{5} and
{\d{2}Some\{33\}\w{5}} parses to \d{2}Some{33}\w{5}

Un-Paired delimiters (raw: \d{2}Some\+33\+\w{5})

+\d{2}Some\+33\+\w{5}+ parses to \d{2}Some+33+\w{5} and
+\d{2}Some\\+33\\+\w{5}+ won't parse because the delimiter is un-escaped.

Exine answered 5/5, 2016 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.