SoFunction
Updated on 2024-11-12

python get email in string

Call the re library to get the email in the string by using compile, findall

import re
email=(r'[a-z0-9\-\.]+@[0-9a-z\-\.]+')
emailtest=r'adfasldfjdsl fan02@'
emailset=set()
for em in (emailtest):
    (em)
for em1 in sorted(emailset):
    print(em1)

Modification:

By calling functions and dynamically assigning values to strings

import re
def emailre(teststr):
    email=(r'[a-z0-9\-\.]+@[0-9a-z\-\.]+')
    emailset=set()  #List
    for em in (teststr):
        (em)
    for eml in sorted(emailset):
        print(eml)
emailtest='sdfdsgf asd03@'
emailre(emailtest)
# or
strtest=r'sdgfsg abc@'
emailre(strtest)

Run results:

Supplement:

Extracts Email from the specified string:

  /**
   * Email from the specified string
   * content The specified string
   */
  public static String parse(String content) {
  String email = null;
  if (content==null || ()<1) {
 return email;
  }
 //Find out the names that contain @
 int beginPos;
 int i;
String token = "@";
String preHalf="";
 String sufHalf = "";

beginPos = (token);
 if (beginPos>-1) {
 //Prior item scanning
 String s = null;
  i= beginPos;
 while(i>0) {
s = (i-1,i);
 if (isLetter(s))
    preHalf = s+preHalf;
  else
    break;
  i--;
  }
 // Posterior item scanning
  i= beginPos+1;
  while( i<()) {
   s = (i,i+1);
   if (isLetter(s))
    sufHalf =  sufHalf +s;
   else
    break;
    i++;  
   }
  //Judgement of legality
  email = preHalf + "@" + sufHalf;
   if (isEmail(email)) {
   return email;
 }
  }
return null;
}

 /**
 * Determine whether it is a legitimate email
* email Email address
/** * email address
public static boolean isEmail(String email) {
 try {
   if (email==null || ()<1 || ()>256) {
   return false;
 }
   
String check = "^([0-9a-zA-Z]+[_.0-9a-zA-Z-]+)@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2,3})$";
 Pattern regex = (check);
 Matcher matcher = (email);
  boolean isMatched = ();
 if(isMatched) {
   return true;
 } else {
  return false;
 }
 } catch (RuntimeException e) {
 return false;
  } 
 }
 
 /**
  * Determine whether the character is legal or not
 * c Characters to be judged
 */
public static boolean isLetter(String c) {
 boolean result = false;
 if (c==null || ()<0) {
  return false;
 }
 //a-z 
 if (("a")>=0 && ("z")<=0) {
  return true;
 }
 //0-9
 if (("0")>=0 && ("9")<=0) {
 return true;
 }
 //. - _
 if ((".") || ("-") || ("_") ) {
 return true;
}
 return result; 
 }

to this article on python to get the string of email is introduced to this article, more related to get the string of email content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!