note 66794 added to reference.pcre.pattern.syntax
| From: | ufotds at yahoo dot com | Date: | Mon, 29 May 2006 15:25:35 +0000 |
| Subject: | note 66794 added to reference.pcre.pattern.syntax | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-112500@lists.php.net to get a copy of this message | ||
Ok, I will try to explain the regex by davout. Most of this is explained above though, but if you
are new to regex and just read all off the above, you probably deserved a practical one nicely
dissected. I dissect it part by part...
%"(.+)"[\s]+((http|https|ftp|file)://[^\s]+?)(.*)%sU
% //this is a delimiter, basically saying to the parser that if it is found again, the expression
stops.
" //this is a html entity encoding, because in html quotes are like metacharacters, so they
are escaped this way. see here for a table http://www.cookwood.com/entities/
( //this is the beginning of the first group, will later be referenced by $1, or (if referenced
within the exp) \1, this doesn't happen here
. //matches any character... (normally except newline unless you set the s modifier in the end,
which happens here, so it is anything including newline)
+ //... one or more times
) //end of first group
" //same as above
[\s] //[]denotes a class, meaning that this place can hold any character from within the class, in
this case it is a metacharacter signifying any whitespace character
+ //...again one or more of the last character
( //open the second group $2
(http|https|ftp|file) //the third group ($3) is an alteration, and can be any of these to fulfill a
match.
:// //they just represent themselves literally
[^\s]+? //this is one or more none(^) whitespace characters, where the the quantifier is greedy this
time, see the section on repetition above how it works, but basically we want the group to
include the whole url, and because the next character (.*) matches anything, an ungreedy
quantifier here would only match one character, because the .* would take over immeadiately.
) //close the second group
(.*) //$4 this basically consumes the rest of the string to put it back after we are done. As
far as I can see, this last group is just redundant, as we don't replace this, we just
put it back. Personally I would leave this away.
% //end of expression
s //make a . fit even newline
U //Reverses default to ungreedy, and +? or ?? or *? to greedy.
The groups are then used to replace the pattern and put these pieces back... So to make a long story
short. you can write your link like "name" http://my.link bla
bla bla. and it will be replaced by a real one.
So, that's how easy it is... Have fun
----
Server IP: 82.146.33.222
Probable Submitter: 157.193.107.67
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/reference.pcre.pattern.syntax.php
Edit -- http://master.php.net/note/edit/66794
Del: integrated -- http://master.php.net/note/delete/66794/integrated
Del: useless -- http://master.php.net/note/delete/66794/useless
Del: bad code -- http://master.php.net/note/delete/66794/bad+code
Del: spam -- http://master.php.net/note/delete/66794/spam
Del: non-english -- http://master.php.net/note/delete/66794/non-english
Del: in docs -- http://master.php.net/note/delete/66794/in+docs
Del: other reasons-- http://master.php.net/note/delete/66794
Reject -- http://master.php.net/note/reject/66794
Search -- http://master.php.net/manage/user-notes.php