/**
* Extract email from the specified string
* String specified by content
*/
Public static string parse (string content ){
String email = NULL;
If (content = NULL | content. Length () <1 ){
Return email;
}
// Find @
Int beginpos;
Int I;
String token = "@";
String prehalf = "";
String sufhalf = "";
Beginpos = content. indexof (token );
If (beginpos>-1 ){
// Previous scan
String S = NULL;
I = beginpos;
While (I> 0 ){
S = content. substring (I-1, I );
If (isletter (s ))
Prehalf = S + prehalf;
Else
Break;
I --;
}
// Post-item Scan
I = beginpos + 1;
While (I <content. Length ()){
S = content. substring (I, I + 1 );
If (isletter (s ))
Sufhalf = sufhalf + S;
Else
Break;
I ++;
}
// Determine validity
Email = prehalf + "@" + sufhalf;
If (isemail (email )){
Return email;
}
}
Return NULL;
}
/**
* Check whether the email address is valid.
* Email address
*/
Public static Boolean isemail (string email ){
Try {
If (email = NULL | email. Length () <1 | email. Length ()> 256 ){
Return false;
}
String check = "^ ([0-9a-za-z] + [_. 0-9a-za-z-] +) @ ([a-zA-Z0-9-] + [.]) + ([A-Za-Z] {2, 3}) $ ";
Pattern RegEx = pattern. Compile (check );
Matcher = RegEx. matcher (email );
Boolean ismatched = matcher. Matches ();
If (ismatched ){
Return true;
} Else {
Return false;
}
} Catch (runtimeexception e ){
Return false;
}
}
/**
* Determine whether a character is valid
* C characters to be judged
*/
Public static Boolean isletter (string c ){
Boolean result = false;
If (C = NULL | C. Length () <0 ){
Return false;
}
// A-z
If (C. comparetoignorecase ("A")> = 0 & C. comparetoignorecase ("Z") <= 0 ){
Return true;
}
// 0-9
If (C. comparetoignorecase ("0")> = 0 & C. comparetoignorecase ("9") <= 0 ){
Return true;
}
//.-_
If (C. Equals (".") | C. Equals ("-") | C. Equals ("_")){
Return true;
}
Return result;
}