tags:

views:

58

answers:

6

I am trying to match a list of colon separated emails. For the sake of keeping things simple, I am going to leave the email expression out of the mix and match it with any number of characters with no spaces in between them.

The following will be matched...

somevalues  ;somevalues;       somevalues;

or

somevalues; somevalues             ;somevalues

The ending ; shouldn't be necessary.

The following would not be matched.

somevalues ;   some values    somevalues;

or

some values; somevalues some values

I have gotten this so far, but it doesn't work. Since I allow spaces between the colons, the expression doesn't know if the space is in the word, or between the colon.

([a-zA-Z]*\s*\;?\s*)*

The following is matched (which shouldn't e)

somevalue ; somevalues  some values;

How do I make the expression only allow spaces if there is a ; to the left or right of it?

+5  A: 

Why not just split on semi colon and then regex out the email addresses?

matt-dot-net
If I'm not mistaken a semicolon is a valid character in RFC822 email addresses so you should be careful when using this that the emails are guaranteed not to contain any semicolons.
Mark Byers
interesting - wonder how my outlook handles that. I think RFC2822 has superceded but I haven't read that one either.
matt-dot-net
+1  A: 

I do not know what language you are using, but all the major languages allow the split() method, which basically breaks down a string into an array given a set of characters which act as a delimiter so that the method 'knows' where it is going to split the string.

Also, there is another method known as trim(). What this method does is that it removes white spaces before and after a string. So basically, what you might want to do is to first split your string, then, for each item that you have from the broken down string, trim it and then use the regular expression to see if it fits.

npinti
A: 

The problem comes from the ? in \;?

[a-zA-Z]*(\s*;\s*[a-zA-Z]*)*

should work.

mb14
A: 

Try

([a-zA-Z]+\s*;\s*)*([a-zA-Z]+\s*\)?

Note that I changed * to + on the e-mail pattern since I assume you don't want strings like ; to match.

Adam Crume
A: 

This following PCRE Expression should work.

\w+\s*(?:(?:;(?:\s*\w+\s*)?)+)?

However if putting the email address validation regular expression on this will require replacing \w+ with (?:<your email validation regex>)

Probabbly This is exactly what you want, tested on http://regexr.com?2rnce

EDIT: However depending on the language you might? need to escape ; as \;

This is exactly what I am looking for. I am using the String.Split in c# but I wanted to validate the textbox on client side before I do anything for performance reasons.
Paul Knopf
Can you post the final regex with email address validation ??
A: 

here you can know everything you need about regex click here

Saher