Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "~" character signify in PHP regex?

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 $.

like image 209
Theramax Avatar asked Feb 07 '14 09:02

Theramax


People also ask

What does character do in regex?

\w (word character) matches any single letter, number or underscore (same as [a-zA-Z0-9_] ). The uppercase counterpart \W (non-word-character) matches any single character that doesn't match by \w (same as [^a-zA-Z0-9_] ). In regex, the uppercase metacharacter is always the inverse of the lowercase counterpart.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1. 1* means any number of ones.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

What does the plus character [+] do in regex?

The plus ( + ) is a quantifier that matches one or more occurrences of the preceding element. The plus is similar to the asterisk ( * ) in that many occurrences are acceptable, but unlike the asterisk in that at least one occurrence is required.


2 Answers

It is a delimiter

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

like image 138
Nambi Avatar answered Sep 23 '22 10:09

Nambi


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.

like image 42
hek2mgl Avatar answered Sep 25 '22 10:09

hek2mgl