Log in

View Full Version : PCRE expressions tutorial



Jas
01-30-2008, 04:08 AM
Sorry to start another thread (I think I have 3 going in this section), but I am getting ready to start a forum for my site, and. . .

I am looking for tutorials on the PCRE expressions used in PHP functions like preg_match(). I found this one (http://gnosis.cx/publish/programming/regular_expressions.html) which got me a great start, but I couldn't find any others that were useful. Any suggestions would be great, because I know next to nothing about it. I still don't get some of the things in the tutorial.

james438
01-30-2008, 07:17 AM
Funny, I was just updating a couple of tutorial PCRE articles on my site that deal specifically with PCRE, but so as not to promote my own site I would recommend the following sites that I have looked at to further my understanding of PCRE or that I have used as a reference when writing a PCRE script.

Perl Tutorial (http://zvon.org/other/PerlTutorial/Output/regexps.html) This site was a great start for me. It is a Perl tutorial, but Perl and PCRE are nearly identical hence Perl Compatible Regular Expressions.
html entities list (http://www.w3schools.com/tags/ref_entities.asp) Meh, this link has it's uses. I use it for when I am converting the format of a string to html entities.
Regular Expression Library (http://regexlib.com/default.aspx) For the advanced user this is a site that specifically deals with regular expressions.
PCRE syntax (http://us.php.net/manual/en/reference.pcre.pattern.syntax.php) A very important page on the php.net site that explains PCRE creation.
PCRE modifiers (http://us.php.net/manual/en/reference.pcre.pattern.modifiers.php) If you need to modify how PCRE looks for matches look at this page on the php.net website. You may not be using this page very often, but be sure to look it over, because you will probably find yourself needing it at some point in the future.
octal code calculator (http://nickciske.com/tools/octal.php) Here is a page to help you find the octal code for a space or bracket or other symbol or tab or letter.

Hope these help.

As a quick side note. I wrote the articles on PCRE for my site because I found some of it hard to understand and figured I could word it better for people like myself who have an intermediate level of PHP skill. It starts simple and gets more advanced with lots of examples. The first and fourth links above are easily the best.

Jas
01-30-2008, 08:04 PM
Thank you! I'll be using those alot, I think. But what's the URL to the tutorials on your website?

james438
01-30-2008, 08:59 PM
Hi, I don't know the protocol for this, so I will try to play it safe and send you a PM listing the links to my site.

Jas
01-31-2008, 02:43 AM
Thank you very much! I just skimmed your site a bit, but it looks really useful.

Hi, I don't know the protocol for this, so I will try to play it safe and send you a PM listing the links to my site.

I don't think it's a problem to post a link to your site when your helping someone, but it's best not to chance it I suppose.

Thanks again! :)

Twey
01-31-2008, 01:07 PM
Hi, I don't know the protocol for this, so I will try to play it safe and send you a PM listing the links to my site.It's fine to link to your site so long as it's relevant/useful and not just spam.
Perl and PCRE are nearly identicalNot true... Perl is a common language of which native regular expression support is only one feature.
Here is a page to help you find the octal code for a space or bracket or other symbol or tab or letter.Why would you need it? PCRE support hex character codes.

Jas
01-31-2008, 04:16 PM
It's fine to link to your site so long as it's relevant/useful and not just spam.
Thought so, and that makes sense. The point is to prevent people from advertising on the site.


Why would you need it? PCRE support hex character codes.

It has those, as well binary.

james438
01-31-2008, 05:35 PM
It's fine to link to your site so long as it's relevant/useful and not just spam.Thanks for the tip. I wasn't sure.

Why would you need it? PCRE support hex character codes.
You're right. I can't think of a good reason to use octal code. Once upon a time I thought I needed it for something like:

<?php
$string="qqertqqqbbbddqqertq";
$r="\336";
$string = preg_replace('/[bd]/', $r, $string);
echo "$string";
?>

But I know now that that is due to the ftp program that I was/am using. That and I see little reason to want to replace a pattern with something like a tab, but octal code should do the trick and I do not know of another way to do that. I seem to remember needing to use octal code for my scripts when nothing else would work. Too long ago for me I'm afraid.


Perl and PCRE are nearly identical ok, the regular expression aspect of Perl is nearly identical to PCRE, but I thought that was implied when I said
hence Perl Compatible Regular Expressions probably not though ;)