javamatch
㈠ java matches()與equals()有什麼區別
matches: 判斷字元是否與指定的 正則表達式 相匹配
equals : 判斷兩個字元串是否相等
舉例:
1. matches
String str = "123abc";
String regex = "^[0-9]+[a-c]+$";
System.out.println(str.matches(regex));
2. equals
String str1 = "ABC";
String str2 = "ABC";
System.out.println(str1.equals(str2));
一個是判斷是否符合正則表達式規則,另一個是判斷是否兩個字元串相等
㈡ 如何用java實現javascript的match效果
Java 的正則表達式用法和 JavaScript 的類似。
但是正則表達式的寫法略有不同。在 Java 中正則表達式源是當中普通字元串來對待的,不需要像 JavaScript 那樣用「/.../」包裹,另外,特別要注意正則表達式轉義字元是雙斜杠(因為在 Java 中,斜杠已是字元串的轉義字元,雙斜杠才代表斜杠)。
至於使用的語法,常規情況下,需要用到 Pattern 類,下面是一個例子:
importjava.util.regex.Pattern;//導入regex包
importjava.util.regex.Matcher;
//publicclass...{
Patternp=Pattern.compile("Hello\w");//編譯正則表達式
Matcherm=p.matcher("HelloWorld");//開始匹配字元串
while(m.find()){//只要匹配則循環
Stringresult=m.group();//返回匹配的子串
}
//}
但是有時候不用這么麻煩,String 類中有隱含的正則表達式匹配函數 matches() 和 split(),其中 split() 和 JavaScript 中的 match() 比較相似。下面是例子:
Strings="HelloAHelloBHelloC";
booleanbResult=s.matches("Hello[A-C]");//matches()僅返回邏輯判斷結果
String[]strResults=s.split("Hello[A-C]");//split()可返回結果子串,放在數組當中
詳細用法請參考 Java 語言手冊。
http:// docs.oracle.com/javase/6/docs/api/java/lang/String.html
http:// docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
㈢ java matches 正則表達式 匹配某個字元恰好出現n次
[2]代表字元串取值范圍,{3}代表字元串的總長度 ,你換成"[2-3]{5}"就OK了
㈣ java中matchs()的用法
我就說,這個問題這么沒人回答,原來是一個這么"泛"的問題,不好回答呀!簡單跟你說一下吧,matchs()這個方法中需要傳入一個參數,這個參數是一個字元串類型的。實際上,在執行的時候,JVM會把這個字元串參數理解成為正則表達式,然後實現正則匹配!正則匹配是一個大的議題,一點點篇幅說不清楚,你可以去想像一下你在使用系統搜索功能的時候,在搜索框中填寫的東西,那個就是正則表達式。然後,還是給你一個示例把:
public class Test{
public static void main(String args[])
{
String str="abcdefg";
//檢查"abcdefg"是否匹配"abcdefg"
System.out.println(str.matches("abcdefg"));
//檢查"abcdefg"是否匹配"abc"
System.out.println(str.matches("abc"));
//檢查"abcdefg"是否匹配 以abc開頭 後接任意字元(可以為空)
System.out.println(str.matches("^abc.*"));
//檢查"abcdefg"是否匹配 以abc開頭,以字母結束(可以為空)
System.out.println(str.matches("^abc[a-z]*"));
//檢查"abcdefg"是否匹配以任意字元開頭,以abc作為結尾
System.out.println(str.matches(".*abc$"));
}
}
執行結果:
true
false
true
true
false
㈤ java matcher方法
看一下Pattern的matcher方法就知道了
/**
* Creates a matcher that will match the given input against this pattern.
* </p>
*
* @param input
* The character sequence to be matched
*
* @return A new matcher for this pattern
*/
public Matcher matcher(CharSequence input) {
if (!compiled) {
synchronized(this) {
if (!compiled)
compile();
}
}
Matcher m = new Matcher(this, input);
return m;
}
它返回的是一個新的Matcher對象, 再執行它的matches()方法.
遇到不懂的時候看看源代碼就一目瞭然啦
㈥ java里.matches方法有什麼用
java.lang包中的String類,java.util.regex包中的Pattern,Matcher類中都有matches()方法。
都與正則表達式有關。下面我分別舉例:(字元串:"abc",正則表達式: "[a-z]{3}")
String類的方法:
boolean b = "abc".matches("[a-z]{3}"
System.out.println(b);
Pattern類中的方法:
boolean b = Pattern.matches("[a-z]{3}","abc");
System.out.println(b);
Matcher類中的方法:
Pattern p = Pattern.compile("[a-z]{3}");
Matcher m = p.matcher("acc");
boolean b =m.matches()
System.out.println(b)
㈦ Java類庫中String類的match方法用法
糾正下,java中用到的是matchs,不是match,意思為比較兩個值是否相等。舉例:
String str = "1111111";
String str1 = "1111111";
System.out.println(str.matches(str1));//兩個輸出的值相等,所以是true
String format = "yyyy-MM-dd hh:mm:ss";
String time = "2010-07-13 03:30:50";
System.out.println(time.matches(format));//比較的是值不是類型,所有是false
輸出結果:
㈧ 請問java高手,如何用String的matches()匹配只有(字母、數字、中文、半形@)的字元串啊感激不盡
packagetestDemo;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;publicclassDemo
{
publicstaticvoidmain(Stringargs[]){//按指定模式在字元串查找
Stringline="1a我@163com";Stringpattern="[a-zA-Z0-9u4E00-u9FA5@]+";
//創建Pattern對象
Patternr=Pattern.compile(pattern);//現在創建matcher對象
Matcherm=r.matcher(line);
if(m.matches()){
System.out.println("inputvlueisOK");
}else{
System.out.println("NOMATCH");
}
}
}
㈨ java正則表達式判斷一個字元前是否有一個字元
java使用match和pattern來實現判斷字元串是否不含有某個字元,實例如下:
/**
* 判斷字元串是否數值
* @param str
* @return true:是數值 ;false:不是數值
* @author:WD_SUHUAFU
*/
private boolean isNumber(String str)
{
Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]*)?$");
Matcher match=pattern.matcher(str);
return match.matches();
}
㈩ java中怎麼匹配字元串
匹配方式(其中每個方式對應RegUtil.java中的一個方法)代碼:
package test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegUtil {
/**
* 方式一:基本正則表達式實現
* @param str
* @param reg
* @return
*/
public static boolean matchMatcher(String str,String reg){
Pattern pattern =Pattern.compile(reg);
Matcher matcher=pattern.matcher(str);
return matcher.matches();
}
/**
* 方式二:利用Pattern自帶實現
* @param str
* @param reg
* @return
*/
public static boolean matchPattern(String str,String reg){
return Pattern.matches(reg, str);
}
/**
* 方式三:String自帶實現
* @param str
* @param reg
* @return
*/
public static boolean matchStr(String str,String reg){
return str.matches(reg);
}
}
對應單元測試代碼如下:
package test;
import org.junit.Assert;
import org.junit.Test;
public class TestReg {
@Test
public void testParttern(){
Assert.assertTrue(RegUtil.matchMatcher("123", "//d+"));
Assert.assertTrue(RegUtil.matchPattern("123", "//d+"));
Assert.assertTrue(RegUtil.matchStr("123", "//d+"));
}
}