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 ~