mIRC Homepage
Posted By: Thray Regex trouble - 07/06/04 09:26 PM
As I've previously mentioned in another post.. Regexes blow my mind. Not good at em.

I was trying to make one to match an email addy and came up with:
^[A-Za-z0-9_\.-]+@[A-Za-z0-9_\.-]+[A-Za-z0-9_][A-Za-z0-9_]$
Now, this is pretty basic stuff, right? I mean, this particular use of a regex. But I can't figure it out.

Oddly enough, it doesn't appear to match email addresses at all! It does.. weird matches. For example, if the letter 'c' is in it, it is always false.

Can anyone please explain what I'm doing wrong?
Posted By: Kelder Re: Regex trouble - 07/06/04 10:19 PM
Google gives this: ^[\w\.\-]+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$

Also: ^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z0-9._%-]{2,4}$
and even ^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$ if you want to allow for ip addresses...

You'll have to put them in a %var first, just putting the above in a $regex will parse incorrectly.
Posted By: qwerty Re: Regex trouble - 07/06/04 10:36 PM
I use this:
Code:
alias isemail var %a = /^[-.\w]+@(?:[a-z\d](?:[-a-z\d]*[a-z\d])?\.)+[a-z]{2,}$/i | return $regex($1,%a)
The hostname matching part is (hopefully) 100% accurate, unlike Google's regex.
© mIRC Discussion Forums