Page 1 of 2
Regular expresions...
Posted: Tue Jun 24, 2003 4:10 pm
by m@ndio
Is it just me or are regexps one of, if not the hardest thing to learn about PHP?? Anyone know any decent tutorials?
Posted: Tue Jun 24, 2003 4:55 pm
by nielsene
Regexp's are not paticular to PHP, most parsing-type languange use them as do many shell programs. I haven't seen many good tutorials on the web, but I do like the O'Reilly Mastering Regular Expressions book. If you think of yourself as a computer scientist and not a programmer, you might want to check out a textbook that covers them. Typically speaking texts that worry about parsing, computability, or automata will cover regular expresssions.
Posted: Tue Jun 24, 2003 5:14 pm
by delorian
Look at this article:
http://www.phpbuilder.com/columns/dario19990616.php3
It's kinda old, but it realy opens your mind.
Posted: Tue Jun 24, 2003 6:40 pm
by nielsene
Yeah, that's a great intro. I had forgotten about it. It doesn't cover forward/back references and "capturing" matches, but its good to start with. If you get through it and still need more complexity then check out the O'Reilly book.
Posted: Wed Jun 25, 2003 2:23 am
by m@ndio
so with regular expressions could you check if the user entered ONLY a-z and 0-9 everything else like '"@{£$%$_£"%" spits back a message saying i duno.. "you have entered invalid characters". How would you do this?
I did this. It sort of worked but gave inconsistant results...
Code: Select all
if(!ereg("[a-zA-Z]{,}", $_POST["username"])){
$errors[] = "You have entered <b>invalid characters</b>";
}
Posted: Wed Jun 25, 2003 2:33 am
by delorian
Code: Select all
eregi("^[a-z0-9]$", $_POST["username"]) // this will check if in the whole string you have a-z A-Z or 0-9
Posted: Wed Jun 25, 2003 3:17 am
by patrikG
I forgot who on this board said this: "Regular expressions were created by evil programmers to give you a headache."
Truer words were never spoken, yet with great headache comes great power

Posted: Wed Jun 25, 2003 4:58 am
by releasedj
Use this:
Code: Select all
if(!eregi("^[a-z0-9]+$", $_POST["username"])){
$errors[] = "You have entered <b>invalid characters</b>";
}
Posted: Wed Jun 25, 2003 8:13 am
by cactus
I have recommended the use of :
Ref
http://www.weitz.de/regex-coach/
Both for learning and debugging regex's, only issue is that it only does Perl Compatible regex's, but great non-the-less.
Regards,
Posted: Wed Jun 25, 2003 9:57 am
by Gleeb
Microsoft have a good reference in the MDSN library that I use. It makes references to Javascript and VBScript, but regex is pretty standardised between platforms.
Posted: Thu Jun 26, 2003 12:27 pm
by m@ndio
oy! you hijacked my post

Posted: Thu Jun 26, 2003 2:24 pm
by twigletmac
m@ndio wrote:oy! you hijacked my post

Sorry 'bout not noticing that before - I've trimmed out the other discussion, time for a little chastisement.
Mac
Posted: Thu Jun 26, 2003 2:26 pm
by m@ndio
cheers twig
Posted: Thu Jun 26, 2003 2:33 pm
by m3rajk
twigletmac wrote:m@ndio wrote:oy! you hijacked my post

Sorry 'bout not noticing that before - I've trimmed out the other discussion, time for a little chastisement.
Mac
oops. i think that was started by something i mentioned. i'm sorry. i thought that my post would be relevant here and since it was in the same vein that it should need a new thread.
i was just trying get help on something similar since it looked like it was along the same lines.
i actually found the write up in the orielly learning pperl really good for learning regular expressions.. so much so that i first learned something else, butt for somereason seem to have overwrote that in my mind and write regular experssions like i'm writing for perl then go find the rules for the language i'm in if it's not perl and make any necessary adjustments
Posted: Thu Jun 26, 2003 2:37 pm
by m@ndio
no worries, I just didn't get the answer I wanted in my post that's all.