正则表达式
<pre><code class="language-java"> /**
* 匹配由数字、26个英文字母或者下划线空格组成的字符串
*
* @param str
* @return
*/
public static boolean isMatchValidator(String str) {
return str.matches(&quot;^[A-Za-z0-9 -]+$&quot;);
}
/**
* 密码由英文字母(字母区分大小写)和数字组成,且长度5~32位!
*
* @param str
* @return
*/
public static boolean isMatchValidator3(String str) {
boolean b = str.matches(&quot;^[A-Za-z0-9]+$&quot;);
return b;
}
/**
* 以英文字母开头,由字母、数字和‘-’组成,且长度2~8位.
*
* @param str
* @return
*/
public static boolean isMatchValidator2(String str) {
boolean b = str.matches(&quot;^[A-Za-z0-9-]+$&quot;);
if (b) {
String s = str.substring(0, 1);
b = s.matches(&quot;^[A-Za-z]+$&quot;);
}
return b;
}</code></pre>
<p>2.多个连续表达符号替换成1个</p>
<pre><code class="language-java">String s = &quot;+++++++++===========&quot;;
String s1 = s.replaceAll(&quot;[+]+&quot;, &quot;+&quot;).replaceAll( &quot;[=]+&quot;,&quot;=&quot;);
String str = &quot;aaaa bbbbbb &quot; ;
String str1 = str.replaceAll(&quot;\\s+&quot;,&quot;-&quot;);
//String str1 = str.replaceAll(&quot;\\s*&quot;,&quot;-&quot;);
String[] str2 = str.split(&quot;\\s+&quot;);</code></pre>