RegEx question

der.hans PLUGd at LuftHans.com
Thu Sep 17 11:06:41 MST 2009


Am 17. Sep, 2009 schwätzte Paul Mooring so:

> Thanks for the help I did get it working with this:
>  name = row[0].match(/^([^@]+)/).to_s
>  domain = row[0].match(/[^@]+$/).to_s
> though I'm not sure I fully understand what the [^@] does. I know that ^
> is the start of the line and + means once or more, and I thought that []
> was used to match any of several characters, but it looks like something
> else is going on here.  Is this working just a fluke or is that the
> proper wat to do it?

^ does match the beginning of a line when not used in a grouping (
called bracket expression in the man page ). In a grouping it means not as
in "not any of these characters".

[^[:digit:]] == not a number
[^^] == not a ^  :)
[^[:digit:]xyz] == not a number, x, y or z

So your first match looks for the beginning of a line followed by one or
more characters that are not an @ and puts that into the first back
reference variable.

Your second match looks for the longest string with no @ in it at the end
of the string.

I highly recommend commenting regular expressions. Even ones that many
might consider plain and obvious.

Describe the expected input. In your case it looks like you're expected
standard, naked email addresses with no comments, angle brackets, etc.

Describe what you're trying to match, so the user portion of the email
address and the domain portion of the email address.

Describe what each regex does and why. This latter part really helps you
gain understanding in regular expressions :).

BTW, man regex.

There's also a glob man page. Read them on different days :).

ciao,

der.hans
-- 
#  http://www.LuftHans.com/        http://www.ABLEconf.com/
#  Director of Engineering, FonWallet Transaction Solutions, Inc.
#  ABLEconf: Saturday, 2009Okt24, Tempe. Call for Presentations now open.
#  We now return you to your regularly scheduled paranoia...


More information about the PLUG-discuss mailing list