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

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

Moderator: General Moderators

Postby php_wiz_kid » Sun Nov 27, 2005 2:15 am

Wonderful tutorial. Espcially to regex noobs like me. This got my form validation working. Good job.
php_wiz_kid
Forum Contributor
 
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Postby infolock » Sun Nov 27, 2005 2:46 am

greatest tutorial on regex on the net imo.

great job.
"Technological progress is like an axe in the hands of a pathological criminal." -- Albert Einstein
firefox users : I have a secret message for you . It is :
data:text/plain;base64,QUxMIFlPVVIgQkFTRSBBUkUgQkVMT05HIFRPIFVTIQo=
User avatar
infolock
DevNet Resident
 
Posts: 1704
Joined: Wed Sep 25, 2002 7:47 pm

Postby Luke » Fri Dec 16, 2005 2:53 pm

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?
My Personal Blog :: My Resume
Clever got me this far... then tricky got me in
Luke Visinoni luke d0t visinoni at gmail d0t com
User avatar
Luke
The Ninja Space Mod
 
Posts: 6368
Joined: Fri Aug 05, 2005 1:53 pm
Location: Chico, CA

Postby twigletmac » Fri Dec 16, 2005 3:26 pm

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

Postby julian_lp » Fri Jul 28, 2006 6:24 pm

infolock wrote:greatest tutorial on regex on the net imo.

great job.



Absolutely true. I ask myself why I didn't find it before.
User avatar
julian_lp
Forum Contributor
 
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Postby Shendemiar » Sun Aug 06, 2006 6:16 pm

Actually, this is best Regexp tutorial i've come across with!

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

Syntax: [ Download ] [ Hide ]
 
        # 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
Syntax: [ Download ] [ Hide ]
 
#\s{0,}\r\n\s{0,}#
 


in php it needs to be
Syntax: [ Download ] [ Hide ]
 
#\\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
Shendemiar
Forum Contributor
 
Posts: 404
Joined: Thu Jan 08, 2004 9:28 am
Location: Helsinki

Postby Chris Corbyn » Mon Aug 07, 2006 5:38 am

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:

Syntax: [ Download ] [ Hide ]
var re = /patterns?/; //No quotes needed so only one \ needed to escape


Perl is also friendly in this sense too:

Syntax: [ Download ] [ Hide ]
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
Chris Corbyn
Breakbeat Nuttzer
 
Posts: 13081
Joined: Wed Mar 24, 2004 8:57 am
Location: Melbourne, Australia

Postby feyd » Mon Aug 07, 2006 8:10 am

That's why I use single quotes for my regex pattern strings.. almost all strings really.
User avatar
feyd
Neighborhood Spidermoddy
 
Posts: 31559
Joined: Mon Mar 29, 2004 4:24 pm
Location: Germantown, Tennessee, USA

It still works

Postby aqualodge » Thu Mar 08, 2007 4:34 am

Great tutorial ... Really helps. Your'e all experts.. :D
aqualodge
Forum Newbie
 
Posts: 1
Joined: Wed Feb 21, 2007 9:40 pm

Excelent tutorial here

Postby yacahuma » Tue Aug 21, 2007 1:30 pm

This is an excellent tutorial.

[url]
http://www.webcheatsheet.com/php/regula ... ssions.php
[/url]
User avatar
yacahuma
Forum Regular
 
Posts: 734
Joined: Sun Jul 01, 2007 7:11 am

Re: Excelent tutorial here

Postby beemzet » Sun Sep 02, 2007 11:04 pm

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

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

Postby GeertDD » Wed Sep 26, 2007 1:35 pm

d11wtq wrote:
Syntax: [ Download ] [ Hide ]
Character         Matching

. (dot)           ANY single character at all

Syntax: [ Download ] [ Hide ]
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?
User avatar
GeertDD
Forum Contributor
 
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

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

Postby crespowu » Thu Jan 17, 2008 3:56 am

Great content.
crespowu
Forum Newbie
 
Posts: 1
Joined: Thu Jan 17, 2008 3:53 am

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

Postby batfastad » Thu Jan 22, 2009 8:47 am

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
batfastad
Forum Contributor
 
Posts: 409
Joined: Tue Mar 30, 2004 5:24 am
Location: London, UK

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

Postby papa » Thu Jan 22, 2009 9:02 am

Very nice tutorial!
User avatar
papa
Forum Regular
 
Posts: 954
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

PreviousNext

Return to Regex

Who is online

Users browsing this forum: No registered users and 1 guest