PDA

View Full Version : Regular expressions


Dennis Wrenn
Wed 24th Jan '01, 11:03pm
Hello,
Can somebody please explain these to me??

I can't find very much about them in the PHP manual.


Thanks,
Dennis

leadZERO
Thu 25th Jan '01, 7:53am
http://www.php.net/distributions/bigmanual.html#ref.pcre
http://www.php.net/distributions/bigmanual.html#ref.regex

Dennis Wrenn
Thu 25th Jan '01, 9:43am
Thanks, but I still don't completely understand them...

I mean.. how do you use them??

I know how to use ^ and $... but thats it..


Thanks,
Dennis

JohnM
Thu 25th Jan '01, 1:29pm
There's a tutorial on PHPBuilder: http://www.phpbuilder.com/columns/dario19990616.php3

Basic stuff: anything inside ( )'s will be returned for use in the replace part (via \\1, \\2, etc)
. means any character
* means 0 or more of the previous
+ means 1 or more of the previous

then after the closing delimiter


$result = preg_replace("/vBoard/flags","vBulletin",$string);


s means search
i means case insensitive
U means ungreedy.

(if U isn't specified:
/<i>(.*)</i>/
will match the first <i> and the last </i>
with U, it'll function correctly)

Dennis Wrenn
Thu 25th Jan '01, 6:20pm
thanks... but what are the / / for?

and what does "Ungreedy" do??

and how do you use s?



Thanks,
Dennis

PS:

That tutorial on PHP Builder was a little confusing.

leadZERO
Thu 25th Jan '01, 7:22pm
The outside / /'s are just enclosing the expression, and they can actually be any of a set of characters.

Dennis Wrenn
Thu 25th Jan '01, 7:56pm
Thanks :)

JohnM
Fri 26th Jan '01, 2:08am
Originally posted by JohnM
(if U isn't specified:
/<i>(.*)</i>/
will match the first <i> and the last </i>
with U, it'll function correctly)