[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

regular expression

Hi,
what is the regular expression of:

email that contain any of the following characters:

edu,  gov,   wa,  sa,  vic,  nsw,  qld,  tas,  catholic
0
kecoak
Asked:
kecoak
  • 3
  • 3
1 Solution
 
ahoffmannCommented:
/(edu|gov|wa|sa|vic|nsw|qld|tas|catholic)+/
0
 
keteracelCommented:
a more complete one would be:

<?php

function checkEmail($email) {
      if (preg_match("/^[^@]+@[\.\w\d-_]+?\.(edu|gov|wa|sa|vic|nsw|qld|tas|catholic)/", $email)) {
              echo "<font color=\"green\">$email</font><br />";
      }
      else {
              echo "<font color=\"red\">$email</font><br />";
      }
}

$emails = array();
$emails[] = "keteracel@berkeley.edu";
$emails[] = "www.berkeley.edu";
$emails[] = "keteracel@edu.com";
$emails[] = "keteracel@business.gov.uk";
$emails[] = "keteracel@business@foo.gov.uk";

foreach($emails as $email) {
      checkEmail($email);
}

?>

this allows .gov.uk, etc... If you don't want this, just put a \$ at the end of the regular expression.

keteracel
0
 
ahoffmannCommented:
hmm, as I read the question, following must match too

   wasa@some.where
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
keteracelCommented:
I assumed that since the question said EMAIL ADDRESS the string: "I am an edu", which is not an email address, should not be matched like it would in your solution. Also, a + means one or more, surely you only need match once?

The question statement is very vague and could do with clarification. I assumed it was looking for part of the domain not just anywhere. Would be good if the question poster clarified this and if ahoffman did not flame other peoples answers.
0
 
ahoffmannCommented:
sorry no offence meant, I just explained why I gave a lazy regex

> Also, a + means one or more, surely you only need match once?
another one to give more details about the requirements ;-)
How about
  education@gov.org.au
  wasa-education@org.au
  mate@cancertas.org.au
 ...
0
 
keteracelCommented:
No probs mate, no offence taken.

kecoak, with regular expressions you need to tell us the exact requirement as we are pattern matching. As ahoffmann has shown, if you just want to know if the strings occur anywhere in the email then his solution will work. If, however, you want them anywhere in a valid email address, or if you want them in the domain part of the email address it won't work (nor will mine in all but the latter case).
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now