(a) Regex CRASH Course! (Pt. 1)
Moderator: General Moderators
-
- Forum Contributor
- Posts: 181
- Joined: Tue Jun 24, 2003 7:33 pm
great tutorial! my only question...
Why does it have to be delimited? What does that mean?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#
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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 (/).The Ninja Space Goat wrote:Why does it have to be delimited? What does that mean?
MacTo 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#
-
- Forum Contributor
- Posts: 404
- Joined: Thu Jan 08, 2004 8:28 am
Actually, this is best Regexp tutorial i've come across with!
I made this in notime, and just 15 minutes ago it seemed impossible!
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
in php it needs to be
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);
So in other words, if your regexp is
Code: Select all
#\s{0,}\r\n\s{0,}#
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Well spotted ~ShendemiarShendemiar 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) \

Code: Select all
var re = /patterns?/; //No quotes needed so only one \ needed to escape
Code: Select all
if ($string =~ /sun(?!shine)/i)
{
print "$string contains the word "sun" but not the word "sunshine"";
}
It still works
Great tutorial ... Really helps. Your'e all experts.. 

Re: (a) Regex CRASH Course! (Pt. 1)
Note that the above is not quite true.d11wtq wrote:Code: Select all
Character Matching . (dot) ANY single character at all
Code: Select all
Modifier Effect s Ignore whitespace
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)
Great content.
Re: (a) Regex CRASH Course! (Pt. 1)
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
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)
Very nice tutorial!