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

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

Wonderful tutorial. Espcially to regex noobs like me. This got my form validation working. Good job.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

greatest tutorial on regex on the net imo.

great job.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post 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.
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post 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,}#
 
Last edited by Shendemiar on Fri Apr 25, 2008 12:35 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That's why I use single quotes for my regex pattern strings.. almost all strings really.
aqualodge
Forum Newbie
Posts: 1
Joined: Wed Feb 21, 2007 8:40 pm

It still works

Post by aqualodge »

Great tutorial ... Really helps. Your'e all experts.. :D
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Excelent tutorial here

Post by yacahuma »

beemzet
Forum Newbie
Posts: 6
Joined: Tue Jun 05, 2007 7:55 am

Re: Excelent tutorial here

Post by beemzet »

User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

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

Post 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?
crespowu
Forum Newbie
Posts: 1
Joined: Thu Jan 17, 2008 2:53 am

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

Post by crespowu »

Great content.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

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

Post 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
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

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

Post by papa »

Very nice tutorial!
Post Reply