Regular expression

hey

im looking to write a regular expression for an email address
however, i want the first part of the name to be initials (first initial
and optional middle initial) and a surname

such as

[email protected]

any help would be greatly appreciated

thanks

Hi –

On 13/01/2008, Johnathan S. [email protected] wrote:

any help would be greatly appreciated
What is it you want to do with this?

a=“[email protected]
p “yes” if a.scan(/\w+.(\w+.)??(\w+)??@(\w+)(.*)/).size > 0

And several other variations thereof.

– Thomas A.

grrr…

regex = /^(A(?!.*A)|B(?!.*B)|C(?!.*C)|D(?!.*D.*D|DD(?!.*D)))+$/

[“ABCDD”,“CA”,“CAD”,
“CADD”,“CADDD”,“DAD”,“DADD”,“DADAD”,“ADD”,
“BAC”,“BBAD”,“AABCCD”,
“DADD”,“DADBD”,“ABCDDA”,
“MIKE”].each do |string|
puts “‘#{string}’ => #{string.match(regex)}”
end

======================
‘ABCDD’ => ABCDD
‘CA’ => CA
‘CAD’ => CAD
‘CADD’ => CADD
‘CADDD’ =>
‘DAD’ => DAD
‘DADD’ =>
‘DADAD’ =>
‘ADD’ => ADD
‘BAC’ => BAC
‘BBAD’ =>
‘AABCCD’ =>
‘DADD’ =>
‘DADBD’ =>
‘ABCDDA’ =>
‘MIKE’ =>

On Nov 4, 2010, at 7:06 PM, Kendall G. wrote:

end
‘AABCCD’ =>

Okay, now this is even closer:

puts “‘#{string}’ => #{string.match(regex)}”
‘AABCCD’ =>

Except that “DAD” is legal according to OP’s description. :slight_smile:


Kendall G.
[email protected]

Mike C.

[email protected]

On Thu, Nov 4, 2010 at 5:40 PM, Mike C. [email protected] wrote:

/^(A(?!.*A)|B(?!.*B)|C(?!.*C)|D(?!.*D.*D)|DD(?!.*D))+$/

Mike, I think you deserve a prize.

double grrrrr… (I messed up the parentheses on the last last one)

regex = /^(A(?!.*A)|B(?!.*B)|C(?!.*C)|D(?!.*D.*D)|DD(?!.*D))+$/

[“ABCDD”,“CA”,“CAD”,
“CADD”,“CADDD”,“DAD”,“DADD”,“DADAD”,“ADD”,
“BAC”,“BBAD”,“AABCCD”,
“DADD”,“DADBD”,“ABCDDA”,
“MIKE”].each do |string|
puts “‘#{string}’ => #{string.match(regex)}”
end
On Nov 4, 2010, at 7:14 PM, Mike C. wrote:

end
‘ADD’ => ADD

On Thu, Nov 4, 2010 at 4:53 PM, Mike C. [email protected] wrote:

==============
It does get you closer though. I rarely remember to make use of

Oh… missed that detail… I think this covers the bases…
end
‘DADD’ =>

http://blog.mikecargal.com

Mike C.

[email protected]

On Fri, Nov 5, 2010 at 12:42 AM, Kendall G. [email protected]
wrote:

On Thu, Nov 4, 2010 at 5:40 PM, Mike C. [email protected] wrote:

/^(A(?!.*A)|B(?!.*B)|C(?!.*C)|D(?!.*D.*D)|DD(?!.*D))+$/

Mike, I think you deserve a prize.

The regexp is really cute! I’d just suggest to replace ^$ by \A\z
because otherwise a newline would go undetected.

Kind regards

robert

Johnathan S. wrote in post #616840:

im looking to write a regular expression for an email address
however, i want the first part of the name to be initials (first initial
and optional middle initial) and a surname

such as

[email protected]

Is this a homework question?

Break the problem down. Write regexp expressions which match the
individual bits:

  • any letter followed by a dot
  • a surname - say any sequence of one or more letters
  • an @ sign
  • a domain (depends how strict you want to be - maybe sufficient is to
    match letter, digit, dot or hyphen, one or more times)

and then combine them.

To match any letter, use a range: [a-z]
To match a literal dot, use . (because . matches any character)
To match something 0 or 1 times, follow it by ‘?’
To match something 0 or more times, follow it by ‘*’
To match something 1 or more times, follow it by ‘+’
Use parentheses to make a group - e.g. the way to match the optional
middle initial is
(xxx)?
where xxx is whatever regexp you chose to match a letter followed by a
dot

HTH, but if it doesn’t, find a good regexp primer.

Regards,

Brian.