Use Java to intercept e-mail addresses

Source: Internet
Author: User
// There are the following strings to get the E-MAIL address:
String emailmessage = < Qdragon @ 126 . Com > Q long, long Shao < Longshao @ 126 . Com > ; Hey < Eee@www.com > ;
// The final output is:
Qdragon @ 126 . Com, longshao @ 126 . Com, eee@www.com
The programming method is as follows:


Public   Static String escpmailaddress (string Str)
{
If (Str = Null   | Str. Length () = 0 )
Return   Null ;
// Get the email address after processing, address rules: <qdragon@abc.com> Chinese, <qdragon@abc.com> Chinese
Pattern patter = Pattern. Compile ( < (. *? ) > ); // ? Is the minimum matching
Matcher Mather = Patter. matcher (STR );
Boolean result
=   False ;
Stringbuffer sb
=   New Stringbuffer ();
Int Start, end;
// Loop find E-MAIL address
For (;;)
{
Result
= Mather. Find ();
If ( ! Result)
Break ;
Start
= Mather. Start ();
End
= Mather. End ();
SB. append (Str. substring (START, end). replaceall (
<*> * ,) + ,);

}

Return SB. tostring ();
}


This completes the required functions! Note the following knowledge about regular expressions:

* It indicates matching 0 or more characters. It is greedy (Greedy), that is, matching as many characters as possible ,. * Match as many non-\ r \ n characters as possible

*? It is called a lazy asterisk, that is, to match as few characters as possible, and will immediately check whether the following characters are satisfied. *? If the latter mode is not met, backtracing is performed. Once the latter string satisfies the pattern ABC, it is immediately stopped.

* + I don't know. + And +? . + It is greedy to match one or more characters; +? It is lazy. After matching a character, it will check whether the subsequent characters are satisfied. +? Backend

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.