[pmwiki-users] relative position style

Petko Yotov 5ko at 5ko.fr
Thu May 9 03:45:44 CDT 2013


Mark Lee writes:
> I have been reading about regular expressions. I am wondering why we need  
> "\\" in the pattern  '/\\(:plant\\s*(.*?):\\)/e'? I know that "\(" means the  
> "(" character, but what does "\\(" mean? The extra "\" is also used before  
> "\s" and "\)". Is that part of pmwiki?

Peter has given you an excellent reply but it is even more complicated. PHP  
doesn't remove all single backslashes, and a string wrapped in double quotes  
quotes will have a different number of slashes stripped than the same string  
wrapped in single quotes.

There is one thing which is the same in double and single quotes though: the  
double backslash is always converted to a single slash. So it is really the  
same for PHP in double and single quotes if all your backslashes are  
doubled. So the following are correct

  '/\\(:plant\\s*(.*?):\\)/e' and  '/\(:plant\s*(.*?):\)/e' in single quotes
  "/\\(:plant\\s*(.*?):\\)/e" only this one is correct in double quotes

(There is another difference between single and double quotes, in double  
quotes $variables are expanded, in single they are not, so many times we  
need to use the ones, many other times the others, and sometimes we could  
use any, and sometimes we have and old string quoted one way, then we insert  
a variable and need to change the quotes.)

So, some of us always escape the backslash, because this way we don't have  
to remember if this is inside single or double quotes. This is easier to  
write. This is what is in the PmWiki code.

If you look at some code examples or other programs, you'll see that other  
people do it differently. I think that in the PHP manual on the website,  
they always use single quotes because there are less characters and the code  
is easier to read and probably easier to debug for beginners.

Both ways are correct, as long as your program does what you want. As a  
programmer, you can decide how you work more efficiently.

Petko




More information about the pmwiki-users mailing list