Page 2 of 3

Posted: Sun Nov 27, 2005 1:15 am
by php_wiz_kid
Wonderful tutorial. Espcially to regex noobs like me. This got my form validation working. Good job.

Posted: Sun Nov 27, 2005 1:46 am
by infolock
greatest tutorial on regex on the net imo.

great job.

Posted: Fri Dec 16, 2005 1:53 pm
by Luke
great tutorial! my only question...
One last thing before we build our first regex. Regex needs to be delimited if using Perl style regular expressions (preg_match()) which I strongly advise you do (Note: ereg_...() is not perl style).

To delimit a regex we start and end with the EXACT same character. The two standards are (but you can use most non-alphanumeric characters):
Code:

/pattern/
#pattern#
Why does it have to be delimited? What does that mean?

Posted: Fri Dec 16, 2005 2:26 pm
by twigletmac
The Ninja Space Goat wrote:Why does it have to be delimited? What does that mean?
Delimiters mark the beginning and end of a pattern - if you use pattern modifiers then they would be added after the ending delimiter. The delimiter you'll probably see most often is a forward slash (/).
To delimit a regex we start and end with the EXACT same character. The two standards are (but you can use most non-alphanumeric characters):
Code:

/pattern/
#pattern#
Mac

Posted: Fri Jul 28, 2006 6:24 pm
by julian_lp
infolock wrote:greatest tutorial on regex on the net imo.

great job.

Absolutely true. I ask myself why I didn't find it before.

Posted: Sun Aug 06, 2006 6:16 pm
by Shendemiar
Actually, this is best Regexp tutorial i've come across with!

I made this in notime, and just 15 minutes ago it seemed impossible!

Code: Select all

 
        # Cut out linefeeds and whitespace around them!
        $value = preg_replace("#\\s{0,}\\r\\n\\s{0,}#", "", $value);
 
I think it was not mentioned, that PHP itself, needs to escape few characters as well, like the Regexp escape character \ or ", with the php escape char: (also) \

So in other words, if your regexp is

Code: Select all

 
#\s{0,}\r\n\s{0,}#
 
in php it needs to be

Code: Select all

 
#\\s{0,}\\r\\n\\s{0,}#
 

Posted: Mon Aug 07, 2006 5:38 am
by Chris Corbyn
Shendemiar wrote:I think it was not mentioned, that PHP itself, needs to escape few characters as well, like the Regexp escape character \ or ", with the php escape char: (also) \
Well spotted ~Shendemiar :) That's very true. The same is true in JavaScript when creating a regexp object by using the syntax var re = new RegExp();. JavaScript is a little more friendly however since you don't need to use a string to make a pattern, you can do it literally:

Code: Select all

var re = /patterns?/; //No quotes needed so only one \ needed to escape
Perl is also friendly in this sense too:

Code: Select all

if ($string =~ /sun(?!shine)/i)
{
    print "$string contains the word "sun" but not the word "sunshine"";
}
PHP however always requires the use of a string.

Posted: Mon Aug 07, 2006 8:10 am
by feyd
That's why I use single quotes for my regex pattern strings.. almost all strings really.

It still works

Posted: Thu Mar 08, 2007 3:34 am
by aqualodge
Great tutorial ... Really helps. Your'e all experts.. :D

Excelent tutorial here

Posted: Tue Aug 21, 2007 1:30 pm
by yacahuma

Re: Excelent tutorial here

Posted: Sun Sep 02, 2007 11:04 pm
by beemzet

Re: (a) Regex CRASH Course! (Pt. 1)

Posted: Wed Sep 26, 2007 1:35 pm
by GeertDD
d11wtq wrote:

Code: Select all

Character         Matching

. (dot)           ANY single character at all

Code: Select all

Modifier         Effect

s                Ignore whitespace
Note that the above is not quite true.

The dot metacharacter does match any single character except for newlines.

I don't know exactly what you mean by 's modifier ignores whitespace'? Anyway, the s modifier just changes the meaning of the dot metacharacter which will then match newlines as well. Agree?

Re: (a) Regex CRASH Course! (Pt. 1)

Posted: Thu Jan 17, 2008 2:56 am
by crespowu
Great content.

Re: (a) Regex CRASH Course! (Pt. 1)

Posted: Thu Jan 22, 2009 7:47 am
by batfastad
This is a great tutorial. I refer to it all the time.

However I think I've found one slight inaccuracy regarding the s modifier which could be updated.

I use the regex coach utility to check out what's happening, and its explanation of the s modifier is "Treat string as single line"
When I was building a regex recently, the tutorial had me wondering why it wasn't working when I used the m modifier as I didn't think the s one was for me. As soon as I switched to the s modifier after looking at that bit on the regex coach, it worked straight away.

Cheers, B

Re: (a) Regex CRASH Course! (Pt. 1)

Posted: Thu Jan 22, 2009 8:02 am
by papa
Very nice tutorial!