Java Regular Expressions for those things

Source: Internet
Author: User

Let's talk about regular expressions today. This is writeProgramWhat we often encounter is a technology that programmers must master. In fact, it is not just java. Any technology is inseparable from regular expressions in any language, and their forms are similar and basically the same.

Let's talk about regular expressions first. Here we recommend one.ArticleHttp://blog.csdn.net/cping1982/article/details/1900808, which detailed introduction of the regular expression of the common methods, can be said to have been very detailed. I will not explain it much.

Below are some common Regular Expressions:

1. Remove spaces on both sides of the string

/**
* Remove spaces on both sides of the string
* @ Param Res string
* @ Return Remove the space string
*/
Public   Static String trimstring (string res ){
String regstartspace =   " ^ \ S * " ;
String regendspace =   " \ S * $ " ;
String regmiddlespace =   " \ S + " ;
// Three replaceall in a row
// The first is to remove the front-end space, and the second is to remove the backend space.
Res = Res. replaceall (regstartspace, "" ). Replaceall (regendspace, "" ). Replaceall (regmiddlespace, "   " );
Return Res;
}

 

2. Whether the matching string contains Chinese Characters

Private String checkchineseorenglish (string content ){
// Todo auto-generated method stub
Pattern = Pattern. Compile ( " [\ U4e00-\ u9fa5] " );
Matcher = Pattern. matcher (content );
If (Matcher. Find ())
{
Return   " Chinese " ;
}
Else
{
Return   " English " ;
}
}

 

3 Remove HTML tags

Pattern = Pattern. Compile ( " <. +?> " , Pattern. dotall );
Matcher = Pattern. matcher ( " <A href =/ " Index.html / " > Homepage </a> " );
String string = Matcher. replaceall ( "" );
System. Out. println (string );

 

4. verify whether the email address is used.

String Str = " Ceponline@yahoo.com.cn " ;
Pattern = Pattern. Compile ( " [// W //. //-] + @ ([// W //-] + //.) + [// W //-] + " , Pattern. case_insensitive );
Matcher = Pattern. matcher (STR );
System. Out. println (matcher. Matches ());

 

Let's talk about this first. Let's try it for the rest of us ~

 

 

 

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.