Extract email from the specified string

Source: Internet
Author: User

Extract email from the specified string

 

  1. Package net. util. Email;
  2. Import java. util. RegEx. matcher;
  3. Import java. util. RegEx. pattern;
  4. Public class emailutil
  5. {
  6. /**
  7. * Extract the specified string of email content from the specified string
  8. */
  9. Public static string parse (string content)
  10. {
  11. String email = NULL;
  12. If (content = NULL | content. Length () <1 ){
  13. Return email;
  14. }
  15. // Find @
  16. Int beginpos;
  17. Int I;
  18. String token = "@";
  19. String prehalf = "";
  20. String sufhalf = "";
  21. Beginpos = content. indexof (token );
  22. If (beginpos>-1 ){
  23. // Previous scan
  24. String S = NULL;
  25. I = beginpos;
  26. While (I> 0 ){
  27. S = content. substring (I-1, I );
  28. If (isletter (s ))
  29. Prehalf = S + prehalf;
  30. Else
  31. Break;
  32. I --;
  33. }
  34. // Post-item Scan
  35. I = beginpos + 1;
  36. While (I <content. Length ()){
  37. S = content. substring (I, I + 1 );
  38. If (isletter (s ))
  39. Sufhalf = sufhalf + S;
  40. Else
  41. Break;
  42. I ++;
  43. }
  44. // Determine validity
  45. Email = prehalf + "@" + sufhalf;
  46. If (isemail (email )){
  47. Return email;
  48. }
  49. }
  50. Return NULL;
  51. }
  52. /**
  53. * Check whether the email address is valid.
  54. */
  55. Private Static Boolean isemail (string email ){
  56. Try {
  57. If (email = NULL | email. Length () <1 | email. Length ()> 256 ){
  58. Return false;
  59. }
  60. String check = "^ ([0-9a-za-z] + [_. 0-9a-za-z-] +) @ ([a-zA-Z0-9-] + [.]) + ([A-Za-Z] {2, 3}) $ ";
  61. Pattern RegEx = pattern. Compile (check );
  62. Matcher = RegEx. matcher (email );
  63. Boolean ismatched = matcher. Matches ();
  64. If (ismatched ){
  65. Return true;
  66. } Else {
  67. Return false;
  68. }
  69. } Catch (runtimeexception e ){
  70. Return false;
  71. }
  72. }
  73. /**
  74. * Determine whether the character is valid. C: the character to be judged.
  75. */
  76. Private Static Boolean isletter (string C)
  77. {
  78. Boolean result = false;
  79. If (C = NULL | C. Length () <0 ){
  80. Return false;
  81. }
  82. // A-z
  83. If (C. comparetoignorecase ("A")> = 0 & C. comparetoignorecase ("Z") <= 0 ){
  84. Return true;
  85. }
  86. // 0-9
  87. If (C. comparetoignorecase ("0")> = 0 & C. comparetoignorecase ("9") <= 0 ){
  88. Return true;
  89. }
  90. //.-_
  91. If (C. Equals (".") | C. Equals ("-") | C. Equals ("_")){
  92. Return true;
  93. }
  94. Return result;
  95. }
  96. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.